Generation of Elementary Discrete Time Sequence With Formula Generation of Elementary Discrete Time Sequence Without Formula
Generation of Elementary Discrete Time Sequence With Formula Generation of Elementary Discrete Time Sequence Without Formula
subplot(2,2,1);
stem(x);
xlabel('n');
ylabel('x(n)');
title('Input sequence');
subplot(2,2,2);
stem(h);
xlabel('n');
ylabel('h(n)');
title('Input sequence');
subplot(2,2,3);
stem(y);
xlabel('n');
ylabel('y(n)');
title('Linear convolution');
subplot(2,2,4);
stem(z);
xlabel('n');
ylabel('z(n)');
title('Circular convolution');
Auto Correlation and cross Auto Correlation and cross
correlation correlation
Without using xcorr With using xcorr
clc;
close all; clc;
clear all; close all;
%Cross correlation without xcorr clear all;
syntax %Cross correlation with xcorr syntax
x=input('Enter the first Sequence : x=input('Enter the first Sequence :
'); ');
h=input('Enter the second sequence : h=input('Enter the second sequence :
'); ');
n=length(x); z=xcorr(x,h);
m=length(h); %auto correlation with xcorr syntax
k=n+m-1; x1=wrev(x)
x1=[x zeros(1,k-n)]'; y=xcorr(x,x1);
h=wrev(h); disp(z);
h=[h zeros(1,k-m)]'; disp(y);
for i=1:k % display the output
c(:,i)=circshift(x1,i-1); subplot(2,3,1);
end stem(x);
y=c*h; xlabel('n');
disp('Cross Correlation of the ylabel('x(n)');
sequences') title('Input x(n)');
disp(y'); subplot(2,3,2);
%Auto correlation without xcorr stem(h);
syntax xlabel('n');
b=x; ylabel('h(n)');
for j=1:length(b) title('Input h(n)');
d(:,j)=circshift(b',j-1); subplot(2,3,3);
end stem(z);
z=wrev(b)'; xlabel('n');
a=d*z; ylabel('y(n)');
disp('Auto Correlation of the title('Cross correlation');
sequences') subplot(2,3,4)
disp(a'); stem(y);
% display the output xlabel('n');
subplot(2,3,1); ylabel('z(n)');
stem(x); title('Auto correlation');
xlabel('n'); subplot(2,3,5);
ylabel('x(n)'); stem(fliplr(z));
title('Input x(n)'); xlabel('n');
subplot(2,3,2); ylabel('y(n)');
stem(h); title('Cross correlation flipped')
xlabel('n');ylabel('h(n)'); subplot(2,3,6);
title('Input h(n)'); stem(fliplr(y));
subplot(2,3,3); xlabel('n');
stem(a'); ylabel('y(n)');
xlabel('n');ylabel('y(n)'); title('Auto correlation flipped')
title('Cross correlation');
subplot(2,3,4)
stem(y');
xlabel('n');ylabel('z(n)');
title('Auto correlation');
subplot(2,3,5);
stem(fliplr(y'));
xlabel('n');ylabel('y(n)');
title('Cross correlation flipped')
subplot(2,3,6);
stem(fliplr(a'));
xlabel('n');
ylabel('y(n)');
title('Auto correlation flipped')
Frequency Analysis using DFT
clc;
clear all;
N=input('Enter the length of sequenc,N=');
m=floor(N/8);
rect=[ones(1,m) zeros(1,N-m)];
subplot(3,1,1);
stem(rect);
%Finding DFT
w=exp(-j*2*pi/N);
n=[0:1:(N-1)];
k=[0:1:(N-1)];
nk=n'*k;
W=w.^nk;
X=rect*W;
%Magnitude and Phase plot
subplot(3,1,2);
stem(k,abs(X(1:N)));
title('Magnitude Sprectrum');
xlabel('Discrete Frequency');
ylabel('Amplitude');
subplot(3,1,3);
stem(k,angle(X(1:N)));
title('Phase Spectrum');
xlabel('Discrete Frequency');
ylabel('Phase Angle');
PROGRAM FOR LOW PASS FILTER:
clc;
close all;
clear all;
N=input('enter the order of the filter=');
fc=input('enter the cutoff frequency=');
fs=input('enter the sampling frequency=');
wc=2*fc*fs; %Normalize the frequency
disp('1-Rectangular 2-Bartlett 3-Hamming 4-Hanning 5-Blackmann=');
%enter the choice of window
choice=input('enter the choice of the window=');
switch(choice)
case{1}
wn=rectangular(N+1);
case{2}
wn=bartlett(N+1);
case{3}
wn=hamming(N+1);
case{4}
wn=hanning(N+1);
case{5}
wn=blackman(N+1);
end
hn=firl(N,wc,wn) %desired impulse response of FIR low pass filter
subplot(2,1,1);
stem(hn); %plot the filter coefficients
grid on;
xlabel('number of samples');
ylabel('amplitude');
title('linear phase FIR filter coefficients');
a=1;
[H,f]=freqz(hn,a,512,fs); %compute the magnitude response of the filter
subplot(2,1,2);
plot(f,20*log10(abs(H))); %plot the magnitude response of FIR LPF
grid on;
xlabel('frequency');
ylabel('magnitude');
title('magnitude response of FIR filter coefficients');
%demonstration of filtering the input
f1=input('enter the frequency of the first signal=');%f2=input('enter the
frequency of the second signal=');
fs=input('enter the sampling frequency to generate samples=');
n=0:(1/fs):.1;
x1=sin(2*pi*f1*n);
x2=sin(2*pi*f2*n);
x=[x1,x2];
y=filter(hn,a,x);%pass the composite signal X through the filter designed
subplot(2,1,1);
plot(x);
%plot the composite signal
xlabel('time');
ylabel('magnitude');
title('composite signal with two frequency components');
subplot(2,1,2);
plot(y);
%plot the filtered signal
xlabel('time');
ylabel('magnitude');
title('filtered signal through the designed LPF');
PROGRAM FOR HIGH PASS FILTER:
clc;
close all;
clear all;
N=input('enter the order of the filter=');
fc=input('enter the cutoff frequency=');
fs=input('enter the sampling frequency=');
wc=2*fc/fs; %Normalize the frequency
disp('1-Rectangular 2-Bartlett 3-Hamming 4-Hanning 5-
Blackmann=');
%enter the choice of window
choice=input('enter the choice of the window=');
switch(choice)
case{1}
wn=rectwin(N+1);
case{2}
wn=bartlett(N+1);
case{3}
wn=hamming(N+1);
case{4}
wn=hann(N+1);
case{5}
wn=blackman(N+1);
end
hn=firl(N,wc,'high',wn) %desired impulse response of FIR
high pass filter
subplot(2,1,1);
stem(hn); %plot the filter coefficients
grid on;
xlabel('number of samples');
ylabel('amplitude');
title('linear phase FIR filter coefficients');
a=1;
[H,f]=freqz(hn,a,512,fs); %compute the magnitude response of
the filter
subplot(2,1,2);
plot(f,20*log10(abs(H))); %plot the magnitude response of FIR
LPF
grid on;
xlabel('frequency');
ylabel('magnitude');
title('magnitude response of FIR filter coefficients');
%demonstration of filtering the input
f1=input('enter the frequency of the first signal=');
%get the frequency of first signal f1<fc
f2=input('enter the frequency of the second signal=');
%get the frequency of second signal f1<fc
fs=input('enter the sampling frequency to generate samples=');
%get the sampling frequency fs should be greater than 2f2
n=0:(1/fs):.1;
x1=sin(2*pi*f1*n);
x2=sin(2*pi*f2*n);
x=[x1,x2];
y=filter(hn,a,x);
%pass the composite signal X through the filter designed
figure;
subplot(2,1,1);
plot(x);
%plot the composite signal
xlabel('time');
ylabel('magnitude');
title('composite signal with two frequency components');
subplot(2,1,2);
plot(y);
xlabel('time');
ylabel('magnitude');
title('filtered signal through the designed HPF');
%Frequency response of digital filter
PROGRAM FOR BAND PASS FILTER:
close all;
clear all;
clc;
fpb=input ('enter the passband frequency=');
fsb=input ('enter the stop band frequency=');
fs=input ('enter the sampling frequency=');
N=input ('enter the order of the filter=');
wp=2*fpb/fs;
ws=2*fsb/fs;
wc=[wp ws];
disp('1-Rectangular 2-Bartlett 3-Hamming 4-Hanning 5-Blackmann=');
%enter the choice of window
choice=input('enter the choice of the window=');
switch(choice)
case{1}
wn=rectwin(N+1);
case{2}
wn=bartlet(N+1);
case{3}
wn=hamming(N+1);
case{4}
wn=hann(N+1);
case{5}
wn=blackman(N+1);
end
hn=fir1(N,wc,'bandpass',wn);
subplot(2,2,1);
stem(hn);
xlabel('munber of samples'); ylabel('amplitude');
title('Lonear phase Fir filter coefficients');
a=1;
[H,f]=freqz(hn,a,512,fs);
subplot(2,2,2);
plot(f,20*log10(abs(H)));
xlabel('frequency'); ylabel('Magnitude');
title('Magnitude response of FIR BPF');
f1=input ('enter the frequency of the first signal=');
f2=input ('enter the frequency of the second signal=');
f3=input ('enter the frequency of the third signal=');
fs1=input ('enter the sampling frequency to generate samples=');
n=0:(1/fs1):.01;
x1=sin(2*pi*f1*n);
x2=sin(2*pi*f2*n);
x3=sin(2*pi*f3*n);
x=[x1 x2 x3];
y=filter(hn,a,x);
subplot(2,2,3);
plot(x);
xlabel('time'); ylabel('Magnitude');
title('composite signal');
subplot(2,2,4);
plot(y);
xlabel('time'); ylabel('Magnitude');
title('filtered signal BPF ');
PROGRAM FOR BAND STOP FILTER:
clc;
close all;
clear all;
N=input('enter the order of the filter=');
fc1=input('enter the passband frequency=');
fc2=input('enter the stopband frequency=');
fs=input('enter the sampling frequency=');
wc1=2*fc1/fs; %Normalize the frequency
wc2=2*fc1/fs;
wc=[wc1 wc2];
disp('1-Rectangular 2-Bartlett 3-Hamming 4-Hanning 5-
Blackmann=');
%enter the choice of window
choice=input('enter the choice of the window=');
switch(choice)
case{1}
wn=rectwin(N+1);
case{2}
wn=bartlett(N+1);
case{3}
wn=hamming(N+1);
case{4}
wn=hanning(N+1);
case{5}
wn=blackman(N+1);
end
hn=firl(N,wc,'stop',wn) %desired impulse response of FIR
band pass filter
subplot(2,1,1);
stem(hn); %plot the filter coefficients
grid on;
xlabel('number of samples');
ylabel('amplitude');
title('linear phase FIR filter coefficients');
a=1;
[H,f]=freqz(hn,a,512,fs); %compute the magnitude response of
the filter
subplot(2,1,2);
plot(f,20*log10(abs(H))); %plot the magnitude response of FIR
BPF
grid on;
xlabel('frequency');
ylabel('magnitude');
title('magnitude response of FIR BSF');
%Demonstration of filtering the input
f1=input('enter the frequency of the first signal=');
f2=input('enter the frequency of the second signal=');
f3=input('enter the frequency of the third signal=');
fs2=input('enter the sampling frequency to generate
samples=');
n=0:(1/fs2):.01;
x1=sin(2*pi*f1*n);
x2=sin(2*pi*f2*n);
x3=sin(2*pi*f3*n);
x=[x1,x2,x3];
y=filter(hn,a,x); figure;
subplot(2,1,1);
plot(x);
xlabel('time');
ylabel('magnitude');
title('composite signal with three frequency components');
subplot(2,1,2);
plot(y);
xlabel('time');
ylabel('magnitude');
title('filtered signal through the designed BSF');