Handout1a MATLAB Tutorial
Handout1a MATLAB Tutorial
• help
The most important function for learning MATLAB on
your own
• To get info on how to use a function:
» help sin
» x=a.data;
» names=a.colheaders;
Writing Excel Files
• You can also write a cell array if you have mixed data:
» C={'hello','goodbye';10,-2;-3,4};
» [s,m]=xlswrite('randomNumbers',C,'mixedData');
• Example
» x=linspace(0,4*pi,10);
» y=sin(x);
• plot generates dots at each (x,y) pair and then connects the dots
with a line
• To make plot of a function look smoother, evaluate at more points
» x=linspace(0,4*pi,1000);
» plot(x,sin(x));
1 1
0.6 0.6
0.4 0.4
0.2 0.2
0 0
-0.2 -0.2
-0.4 -0.4
-0.6 -0.6
-0.8 -0.8
-1 -1
0 2 4 6 8 10 12 14 0 2 4 6 8 10 12 14
Plot Options
• Can change the line color, marker style, and line style by
adding a string argument
» plot(x,y,’k.-’);
• Exponentiation (^)
» 4^2
» (3+4*j)^2
2 2
Operators: standard
Syntax
M = mean(A)
Description
M = mean(A) returns the mean values of the elements along different
dimensions of an array.
mean(Data.^2)
Mean Square
Syntax
Meansqr = mean(Data.^2)
Description
Syntax
V = var(X)
Description
ksdensity
Kernel smoothing density estimate
Syntax
[f,xi] = ksdensity(x)
Description
Syntax
n = hist(Y)
n = hist(Y,x)
Description
n = hist(Y,x) histogram shows the distribution of data values.
Generate and display a bell-curve histogram from We can change the color of the graph and add the edges
Gaussian data: of the bins of white color. This makes the histogram
x = -4:0.1:4; easily seen:
y = randn(10000,1);
hist(y,x) ; h = findobj(gca,'Type','patch');
However, the displayed histogram is crowded and set(h,'FaceColor','r','EdgeColor','w‘);
details can not be shown.
Built-in Functions: Histogram
How to normalize a histogram, so it is a probability
density (how is that the sum of all bins are equal to 1?).
For a probability density function, the integral over the entire space is 1.
Dividing by the sum will not give you the correct density. To get the right
density, you must divide by the area. To illustrate the point, try the following
example.
[f,x]=hist(randn(10000,1),50);%# create histogram from a normal distribution.
g=1/sqrt(2*pi)*exp(-0.5*x.^2);%# pdf of the normal distribution
0.35 0.4
0.35
0.3
0.3
0.25
0.25
0.2
0.2
0.15
0.15
0.1
0.1
0.05 0.05
0 0
-4 -3 -2 -1 0 1 2 3 4 5 -4 -3 -2 -1 0 1 2 3 4 5
Syntax
c = xcorr(x,y,'option')
[c,lags]=xcorr(...)
Description
Syntax
Y = fft(x)
Y = fft(X,n)
Description
However, due to the many limitations of FFT, we usually use the following
procedure to analyze the frequency characteristics:
filter
Filter data with filter object
Syntax
y=filter(hd, x)
Description
Syntax
D = fdesign.bandpass('Fst1,Fp1,Fp2,Fst2,Ast1,Ap,Ast2',
specvalue1,specvalue2,...)
Description
Frequency response
Fst1 — frequency at the edge of the start of the first stop band.
Fp1 — frequency at the edge of the start of the pass band.
Fp2 — frequency at the edge of the end of the pass band.
Fst2 — frequency at the edge of the start of the second stop band.
% Fst1,Fp1,Fp2,Fst2 specified in normalized frequency units.
%% Filter a discrete-time signal with a bandpass filter. The signal is a sum of three
discrete-time sinusoids, π/8, π/2, and 3π/4 radians/sample.
n = 0:159;
x = cos(pi/8*n)+cos(pi/2*n)+sin(3*pi/4*n);
%% Design an FIR equiripple bandpass filter to remove the lowest and highest discretetime sinusoids.
d = fdesign.bandpass('Fst1,Fp1,Fp2,Fst2,Ast1,Ap,Ast2',1/4,3/8,5/8,7/8,60,1,60);
Hd = design(d,'equiripple');
%% pulse response
whitenoise=zeros(size(n));
whitenoise(1)=1; % pulse signal
yn=filter(Hd,whitenoise);
yndft=fft(yn);
figure;
plot(freq,10*log(abs(yndft(1:length(x)/2+1))));
legend('freq response');
xlabel('Freq(Hz)');ylabel('Mag.(dB)')
Example: bandpass filter (result)
90 20
60 -20
Mag.(dB)
Mag.(dB)
50
-40
40
30 -60
20
-80
10
0 -100
0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5
Freq(Hz) Freq(Hz)
Fourier transform of original signal and bandpass signal Frequency response of designed bandpass filter