matlab programs re
TRANSCRIPT
8/8/2019 Matlab Programs Re
http://slidepdf.com/reader/full/matlab-programs-re 1/13
MATLAB PROGRAMS
% ALIASING
clc;
clear all;
t=-10:0.01:10;
T=4; fm=1/T;
x=cos(2*pi*fm*t);
fs1=1.6*fm;
fs2=2*fm;
fs3=8*fm;
n1=-4:1:4;
xn1=cos(2*pi*n1*fm/fs1);
subplot(2,2,1);
plot(t,x);
xlabel('time in sec');
ylabel('x(t)');
title('continious time signal');
subplot(2,2,2);
stem(n1,xn1,'-r');
hold on
plot(n1,xn1);
xlabel('time in sec');
ylabel('x(t)');
title('Discrete time signal with fs<2fm');
n2=-5:1:5;
xn2=cos(2*pi*n2*fm/fs2);
subplot(2,2,3);
sampling frequency lesthan 2fm
sampling frequency equal to 2Fm
sampling frequency greater than 2Fm
n1 value ranges from -4 t0 4
signal with samp freq <2fm
didvides the entire plot into 2 rows and 2 coloumns, and plot the graph in first position
didvides the entire plot into 2 rows and 2 coloumns, and plot the graph in second position
used for ploting the values of t and X in continious form
to show the label in x axis in the figure
to show the label in Y axis in the figure
it gives the title for the figure displayed
used for ploting the values of t and X in discrete form
hold the values as it is
n2 value ranges from -5 t0 5 , i.e n2 = -5,-4,-3,-2,-1,0,1,2,3,4,5
signal with samp freq =2fm
didvides the entire plot into 2 rows and 2 coloumns, and plot the graph in third position
8/8/2019 Matlab Programs Re
http://slidepdf.com/reader/full/matlab-programs-re 2/13
plot(n2,xn2,'-r');
hold on
stem(n2,xn2,'*g');
xlabel('n');
ylabel('x(n)');
title('Discrete time signal with fs=2fm');
n3=-20:1:20;
xn3=cos(2*pi*n3*fm/fs3);
subplot(2,2,4);
plot(n3,xn3);
hold on
stem(n3,xn3,'-r');
xlabel('n');
ylabel('x(n)');
title('Discrete time signal with fs>2fm');
%LINEAR CONVOLUTION
clc;
clear all;
close all;
a=input('enter the 1 seq');
x=input('enter the 2 seq');
b=length(a);
c=x*convmtx(a,b);
disp(c);
l1=0:length(a)-1;
used for ploting the values of n2 and Xn2 in continious form where 'r' represents red colour
used for ploting the values of n2 and Xn2 in discrete form where 'g' represents green colour
signal with samp freq > 2fm
used for ploting the values of n3 and Xn3 in continious
closes the unnecessarywindow
get the 1st sequence at run time
get the 2nd sequence at run time
to find the length of a andstore it in 'b'
used to convolute thesequences a & x
it displays the value of c in comand window
8/8/2019 Matlab Programs Re
http://slidepdf.com/reader/full/matlab-programs-re 3/13
subplot(1,3,1);
stem(l1,a);
xlabel('time');
ylabel('amp');
title('first input sequence');
l2=0:length(x)-1;
subplot(1,3,2);
stem(l2,x);
xlabel('time');
ylabel('amp');
title('second input sequence');
l3=0:length(c)-1;
subplot(1,3,3);
stem(l3,c);
xlabel('time');
ylabel('amp');
title('linear convolution of two sequences');
AT RUN TIME:
Enter the first sequence:[1 2 3 4]
Enter the second sequence:[3 2 1 4]
%CIRCULAR CONVOLUTION
clc;
clear all;
close all;
a=input('enter the first sequence');
x=input('enter the second sequence');
8/8/2019 Matlab Programs Re
http://slidepdf.com/reader/full/matlab-programs-re 4/13
c=ifft(fft(a).*fft(x));
disp('the convoluted sequence');
disp(c);
l1=0:length(a)-1;
subplot(1,3,1);
stem(l1,a);
ylabel('amp');
title('the first squence');
l2=0:length(x)-1;
subplot(1,3,2);
stem(l2,x);
xlabel('time');
ylabel('amp');
title('the second sequence');
l3=0:length(c)-1;
subplot(1,3,3);
st(l3,c);
xlabel('time');
ylabel('amp');
title('circular convolution');
AT RUNTIME
enter the first sequence[1 2 3 4]
enter the second sequence[4 3 2 1]
the convoluted sequence
24 22 24 30
derived from the circular convolution property of DFT
8/8/2019 Matlab Programs Re
http://slidepdf.com/reader/full/matlab-programs-re 5/13
% DOWN SAMPLING
clear all;
N=50;
n=0:1:N-1;
x=sin(2*pi*n/20)+sin(2*pi*n/15);
m=2;
x1=x(1:m:N);
n1=1:1:N/m;
subplot(2,1,1);
stem(n,x);
xlabel('n');
ylabel('X');
title('input sequence');
subplot(2,1,2);
stem(n1,x1);
xlabel('n1');
ylabel('x1');
title('down unsampled sequence');
%ILLUSTRATION OF UP SAMPLING
clear all;
N=10;
n=0:1:N-1;
x=sin(2*pi*n/10)+sin(2*pi*n/5);
L=3;
x1=[zeros(1,L*N)]; here L*N = 30; so it returns the value zero from the range 1 to 30
8/8/2019 Matlab Programs Re
http://slidepdf.com/reader/full/matlab-programs-re 6/13
n1=1:1:L*N; j=1:L:L*N;
x1(j)=x;
subplot(2,1,1);
stem(n,x);
xlabel('n');
ylabel('X');
title('input sequence');
subplot(2,1,2);
stem(n1,x1);
xlabel('n1');
ylabel('x1');
title('up unsampled sequence');
%BUTTERWORTH LPF
clc;
clear all;
close all;
format long;
rp=input('enter the passband ripple:');
rs=input('enter the stop band ripple');
wp=input('enter the pass band frequency');
ws=input('enter the stop band frequency');
fs=input('enter the sampling frequency');
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=buttord(w1,w2,rp,rs); used to find the order of the filter 'n' and cutoff freq wn
8/8/2019 Matlab Programs Re
http://slidepdf.com/reader/full/matlab-programs-re 7/13
[b,a]=butter(n,wn);
wn=(w1*w2);
wn=0:0.01:pi;
[h,o]=freqz(b,a,wn);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(o/pi,m);
xlabel('gain in db');
ylabel('normalised frequency');
subplot(2,1,2);
plot(o/pi,an);
xlabel('normalised freqz');
ylabel('phase in radians');
AT RUNTIME:
enter the passband ripple:.4
enter the stop band ripple30
enter the pass band frequency400
enter the stop band frequency800
enter the sampling frequency2000
% FFT sequence
clc;
clear all;
close all;
a=input('enter the input sequence');
used to find the transfer fncoefficients a and b
8/8/2019 Matlab Programs Re
http://slidepdf.com/reader/full/matlab-programs-re 8/13
N=length(a);
for k=1:N
z(k)=0;
for n=1:N
z(k)=z(k)+[a(n)*exp(-j)*2*pi*(k-1)*(n-1)/n];
end;
c=abs(z);
d=angle(z);
l1=0:length(a)-1;
subplot(2,2,1);
stem(l1,a);
xlabel('time');
ylabel('amp');
title('the input sequence');
l2=0:length(z)-1;
subplot(2,2,2);
stem(l2,z);
xlabel('time');
ylabel('amp');
title('the fft of sequences');
l3=0:length(c)-1;
subplot(2,2,3);
stem(l3,c);
xlabel('time');
ylabel('amp');
title('the magnitude sequence');
l4=0:length(d)-1;
8/8/2019 Matlab Programs Re
http://slidepdf.com/reader/full/matlab-programs-re 9/13
subplot(2,2,4);
stem(l4,d);
xlabel('time');
ylabel('amp');
title('the phase angle sequence');
end;
AT RUNTIME:
Enter the sequence: [1 2 3 4 5]
% FIR LPF USING WINDOWS
clc;
clear all;
wc=.5*pi;
N=25;
alpha=(N-1)/2;
eps=.001;
n=0:1:N-1;
hd=sin(wc*(n-alpha+eps))./(pi*(n-alpha+eps));
wr=boxcar(N); % rectangular window sequence
hn=hd.*wr';%filter coefficients
w=0:.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'-r');
hold on
wh=hamming(N); % rectangular window
hn=hd.*wh'; %filter coefficients
w=0:.01:pi;
8/8/2019 Matlab Programs Re
http://slidepdf.com/reader/full/matlab-programs-re 10/13
h=freqz(hn,1,w);
plot(w/pi,abs(h),'-k');
hold on
wb=blackman(N);
hn=hd.*wb'; %filter coefficients
w=0:.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'-g');
xlabel('Normalised frequency');
ylabel('Magnitude');
title('LPF design using various windows')
hold off;
% FIR HPF USING WINDOWS
clc;
clear all;
wc=.5*pi;% cutoff frequency
N=25;
alpha=(N-1)/2;
eps=.001;
n=0:1:N-1;
hd=(sin(pi*(n-alpha+eps))-sin(wc*(n-alpha)))./(pi*(n-alpha+eps));
wr=boxcar(N); % rectangular window sequence
hn=hd.*wr';%filter coefficients
w=0:.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'-r');
8/8/2019 Matlab Programs Re
http://slidepdf.com/reader/full/matlab-programs-re 11/13
hold on
wh=hamming(N); % rectangular window sequence
hn=hd.*wh'; %filter coefficients
w=0:.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'-k');
hold on
wb=blackman(N); % rectangular window sequence
hn=hd.*wb'; %filter coefficients
w=0:.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'-g');
xlabel('Normalised frequency');
ylabel('Magnitude');
title('HPF design using various windows')
hold off;
% FIR BPF USING WINDOWS
clc;
clear all;
wc1=.25*pi;wc2=.75*pi;% cutoff frequency
N=25;
alpha=(N-1)/2;
eps=.001;
n=0:1:N-1;
hd=(sin(wc2*(n-alpha+eps))-sin(wc1*(n-alpha)))./(pi*(n-alpha+eps));
wr=boxcar(N); % rectangular window sequence
8/8/2019 Matlab Programs Re
http://slidepdf.com/reader/full/matlab-programs-re 12/13
hn=hd.*wr';%filter coefficients
w=0:.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'-r');
hold on
wh=hamming(N); % rectangular window sequence
hn=hd.*wh'; %filter coefficients
w=0:.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'-k');
hold on
wb=blackman(N); % rectangular window sequence
hn=hd.*wb'; %filter coefficients
w=0:.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'-g');
xlabel('Normalised frequency');
ylabel('Magnitude');
title('BPF design using various windows')
hold off;
% FIR Band reject Filter USING WINDOWS
clc;
clear all;
wc1=.25*pi;wc2=.75*pi;% cutoff frequency
N=25;
8/8/2019 Matlab Programs Re
http://slidepdf.com/reader/full/matlab-programs-re 13/13
alpha=(N-1)/2;
eps=.001;
n=0:1:N-1;
hd=(sin(wc1*(n-alpha+eps))-sin(wc2*(n-alpha))+sin(pi*(n-alpha+eps)))./(pi*(n-alpha+eps));
wr=boxcar(N);%rectangular window sequence
hn=hd.*wr';%filter coefficients
w=0:.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'-r');
hold on
wh=hamming(N);%rectangular window sequence
hn=hd.*wh';%filter coefficients
w=0:.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'-k');
hold on
wb=blackman(N);
hn=hd.*wb'; %filter coefficients
w=0:.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'-g');
xlabel('Normalised frequency'); ylabel('Magnitude');
title('BRF design using various windows')
hold off;