Documente Academic
Documente Profesional
Documente Cultură
Matlab Workshop
For representation of signals, the reference axis is of prime importance. Here time
is the independent variable and it serves as the axis for representing time varying
signals.
Axes are created as row arrays in MATLAB, with each point in the array depicting
each instant of time.
For representing time, two main parameters are to be defined.
1. The span- indicates the length.
2. The precision- indicates the distance between two adjacent time points. (This
turns out to be the sampling period of the signal represented.
Syntax:
t=initial point : precision : final point ;
eg: t=0:0.001:1; %This generates a row array of the form 0, 0.001, 0.002, . ,
0.998, 0.999, 1
Plotting a sinusoid
A sinusoid is characterized by its frequency, amplitude and phase.
We generate an array which stores the value of the signal corresponding to each
instant of time defined earlier.
Eg:
t=0:0.001:1;
A=1; %Amplitude of 1
f=2; %frequency of 2 Hz
ph=pi/3; % a phase of 60 degrees
V=A*sin(2*pi*f*t+ph); %sinusoidal values stored in V
Eg.
%subplot
Fs=44000;
t=0:1/Fs:1-1/Fs;
N=length(t);% size of the array 't'
A=1; %Amplitude of 1
f=2; %frequency of 2 Hz
ph=0; % a phase of 60 degrees
V=A*sin(2*pi*f*t+ph); %sinusoidal values stored in V
Vf=fftshift(fft(V));
subplot(2,1,1); plot(t,V); grid on;
axis( [0 1 -2 2]);
xlabel( 'time(s)');
ylabel( 'amplitude');
faxis=-Fs/2:Fs/N:Fs/2-Fs/N;
subplot(2,1,2); plot(faxis,abs(Vf)/(N)); grid on; %complex number so
we plot magnitude spectrum
axis( [-Fs/2 Fs/2-Fs/N -0.1 0.6]);
xlabel( 'Frequency(Hz)');
ylabel( 'amplitude');
3-D plots
%3-D plots
clear all
close all
clc;
%Plotting a sphere of radius 4
%
X.^2+Y.^2+Z.^2=16, Z>0
[X Y]=meshgrid(-4:0.1:4);% generates all the (X,Y) points to be
plotted
Z=sqrt(16-X.^2-Y.^2);
Z=real(Z);
surf(X,Y,Z,'Linestyle', 'none');
axis([-5 5 -5 5 -5 5]);
%since functions allow only one output value for each (x,y)
coordinate, to %represent a sphere we need to plot 2 functions
simultaneously on the same %figure
Z2=-sqrt(16-X.^2-Y.^2);
Z2=real(Z2);
hold on;
surf(X,Y,Z2,'Linestyle', 'none');
Now try viewing the contour along the y-axis. This gives G(jw) and verify
%Plotting s-domain transfer functions
%tf to be plotted ----->
%
s
%
--------------%
(s+1)(s+2)(s+3)
%NB: We are plotting the magnitude contours
clear all;
close all;
clc;
[X,Y]=meshgrid(0:0.01:4);
G=(X.^2+Y.^2)./(((X+1).^2+Y.^2).*((X+2).^2+Y.^2).*((X+3).^2+Y.^2));
surf(X,Y,G,'Linestyle', 'none'); xlabel('x axis'); ylabel('y axis');
% axis([0 5 0 5]);
figure;
X=0; Y=Y(:,1);
G2=(X.^2+Y.^2)./(((X+1).^2+Y.^2).*((X+2).^2+Y.^2).*((X+3).^2+Y.^2));
plot(Y,G2); grid on; xlabel('jw axis'); ylabel('G');
Filter fundamentals
The design of filters is simplified to a large extent by the use of mathematical tools
like MATLAB. Filters find application in diverse domains. We discuss the
implementation of discrete time filters of finite length.
%Digital Filter Impmlementation
%Filters are used to seperate out certain frequencies from a signal
and
%block other frequencies.
%They are of the types%Lowpass, Highpass, Bandpass, Bandreject
%A filter is described uniquely by its impulse response
%The filter design process involves the development of this impulse
%response satisfying our requirements
Try implementing Bandpass filters and obtain output through the speakers using
audioplayer object.
rlocus(G);%rootlocus plot
F=feedback(G,1,-1);
step(F); % output of the system to a step input
impulse(F);%output of the system to impulse input
bode(F);%bode plot magnitude and phase of the system
10
Histogram of an Image
A histogram is a statistical measure, which indicates how many objects correspond
to a particular value in a function. Here, with reference to images, a histogram
gives the number of pixels corresponding to each intensity value.
%Function for finding the histogram of an image
%Here we define the function histogram() which accepts a grayscale
image
%and returns the histogram output array- each element corresponds to
the
%number of pixels having that intensity
function H=histogram(X)
[M N]=size(X); %M-> No. of Rows, N-> No. of columns
X2=double(X); % convert the data from uint8 to double float type
H=zeros(1,256);
for i=1:M
for j=1:N
H(X2(i,j)+1)=H(X2(i,j)+1)+1; %Calculating the histogram
end
end
11
We see from theory that such a mapping function turns out to be the Cumulative
Distribution Function(CDF) of the histogram. Once the CDF is used to transform
the image, we get better contrast and more details can be picked up from the
image. (Kindly refer notes for more details).
Eg.
% A square law intensity transform.
clear all
close all
clc
x=imread('tire.tif'); % reads in an image to x
%histogram of the input image
H_x=histogram(x);
stem(H_x);
y=sqroot_law(x);
H_y=histogram(y); %histogram of the output image
figure;
stem(H_y);
figure;
subplot(1,2,1); imshow(x);
subplot(1,2,2); imshow(y);
%Function defined for finding the square of each pixel intensity value
%A factor to be noted is that the square law function should be scaled
such that 0 maps to 0 and 255 maps to 255.
%Other similar functions may be defined such as square-root,
logarithmic, exponential etc.
function Y=square_law(X)
[M N]=size(X);
x2=double(X);
Y=[];
for i=1:M
for j=1:N
Y(i,j)=1/255*x2(i,j)*x2(i,j);
end
end
Y=uint8(Y);
end
Eg.2 Histogram Equalization the mapping function is the CDF of the input image
histogram
12
clear all
close all
clc
x=imread('tire.tif'); % reads in an image to im
%histogram of the image
H_x=histogram(x);
stem(H_x);
y=sqroot_law(x);
H_y=histogram(y);
figure;
stem(H_y);
figure;
subplot(1,2,1); imshow(x);
subplot(1,2,2); imshow(y);
cdf_x=cdf_img(x);
figure;
plot((0:255), cdf_x); grid on;
%Histogram Equalization
[M N]=size(x);
x2=double(x);
x_equalised=[];
for i=1:M
for j=1:N
x_equalised(i,j)=cdf_x(x2(i,j)+1);
end
end
x_equalised=uint8(x_equalised);
figure;
subplot(1,2,1); imshow(x);
subplot(1,2,2); imshow(x_equalised);
H_xeq=histogram(x_equalised);
figure;
subplot(2,1,1); stem(H_x);
subplot(2,1,2); stem(H_xeq);
13
UARTs transmit data asynchronously, which means there is no clock signal to synchronize the
output of bits from the transmitting UART to the sampling of bits by the receiving UART.
Instead of a clock signal, the transmitting UART adds start and stop bits to the data packet being
transferred. These bits define the beginning and end of the data packet so the receiving UART
knows when to start reading the bits.
When the receiving UART detects a start bit, it starts to read the incoming bits at a
specific frequency known as the baud rate. Baud rate is a measure of the speed of data
transfer, expressed in bits per second (bps). Both UARTs must operate at about the same baud
rate. The baud rate between the transmitting and receiving UARTs can only differ by about 10%
before the timing of bits gets too far off.
Both UARTs must also be configured to transmit and receive the same data packet structure.
14
15
display('F');
fwrite(ser,'F');
pause(0.3);
end
end
end
end
end
16
Notes
17