[go: up one dir, main page]

0% found this document useful (0 votes)
72 views13 pages

Matlab Programs Re

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 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;sampling frequency lesthan 2fm

fs2=2*fm; sampling frequency equal to 2Fm

fs3=8*fm; sampling frequency greater than 2Fm

n1=-4:1:4; n1 value ranges from -4 t0 4

xn1=cos(2*pi*n1*fm/fs1);
signal with samp freq <2fm
subplot(2,2,1);
didvides the entire plot into 2 rows and 2 coloumns, and plot the graph in first position
plot(t,x); used for ploting the values of t and X in continious form

xlabel('time in sec'); to show the label in x axis in the figure

ylabel('x(t)'); to show the label in Y axis in the figure

title('continious time signal');


it gives the title for the figure displayed
subplot(2,2,2); didvides the entire plot into 2 rows and 2 coloumns, and plot the graph in second position

stem(n1,xn1,'-r');
used for ploting the values of t and X in discrete form
hold on hold the values as it is

plot(n1,xn1);

xlabel('time in sec');

ylabel('x(t)');

title('Discrete time signal with fs<2fm');

n2=-5:1:5; n2 value ranges from -5 t0 5 , i.e n2 = -5,-4,-3,-2,-1,0,1,2,3,4,5

xn2=cos(2*pi*n2*fm/fs2); signal with samp freq =2fm

subplot(2,2,3); didvides the entire plot into 2 rows and 2 coloumns, and plot the graph in third position
plot(n2,xn2,'-r'); used for ploting the values of n2 and Xn2 in continious form where 'r' represents red colour

hold on

stem(n2,xn2,'*g'); used for ploting the values of n2 and Xn2 in discrete form where 'g' represents green colour

xlabel('n');

ylabel('x(n)');

title('Discrete time signal with fs=2fm');

n3=-20:1:20;

xn3=cos(2*pi*n3*fm/fs3); signal with samp freq > 2fm

subplot(2,2,4);

plot(n3,xn3); used for ploting the values of n3 and Xn3 in continious

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; closes the unnecessary


window
a=input('enter the 1 seq');
get the 1st sequence at run time
x=input('enter the 2 seq');
get the 2nd sequence at run time
b=length(a);
to find the length of a and
store it in 'b'
c=x*convmtx(a,b);
used to convolute the
sequences a & x
disp(c);
it displays the value of c in comand window
l1=0:length(a)-1;
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');


c=ifft(fft(a).*fft(x)); derived from the circular convolution property of DFT

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
% 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
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
used to find the transfer fn
coefficients a and b

[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');


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;
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;
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');
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


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;
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;

You might also like