0% found this document useful (0 votes)
122 views11 pages

Generation of Elementary Discrete Time Sequence With Formula Generation of Elementary Discrete Time Sequence Without Formula

The document contains code for generating and plotting various discrete time signals including unit step, unit impulse, ramp, exponential, sinusoidal, and cosine signals both with and without using formulas. It also contains code for linear and circular convolution, autocorrelation and cross correlation both with and without using the xcorr function, frequency analysis using the discrete Fourier transform (DFT), and code for designing and demonstrating low pass and high pass filters.

Uploaded by

babehacker
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views11 pages

Generation of Elementary Discrete Time Sequence With Formula Generation of Elementary Discrete Time Sequence Without Formula

The document contains code for generating and plotting various discrete time signals including unit step, unit impulse, ramp, exponential, sinusoidal, and cosine signals both with and without using formulas. It also contains code for linear and circular convolution, autocorrelation and cross correlation both with and without using the xcorr function, frequency analysis using the discrete Fourier transform (DFT), and code for designing and demonstrating low pass and high pass filters.

Uploaded by

babehacker
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Generation of elementary Generation of elementary

Discrete time sequence with Discrete time sequence without


formula formula
t=input('Enter The Length Of The t=input('Enter The Length Of The
Sequence='); Sequence=');
%Unit Step Signal %Unit Step Signal
a=input('Enter The Value Of A='); a=input('Enter The Value Of A=');
n=0:1:t-1; n=0:1:t-1;
y=1*(n>=0); y=ones(1,t);
subplot(2,3,1); subplot(2,3,1);
stem(n,y); stem(n,y);
xlabel('t'); xlabel('t');
ylabel('x(t)'); ylabel('x(t)');
title('Unit Step Signal'); title('Unit Step Signal');
%Unit Impulse %Unit Impulse
n=-t:1:t; n=-t:1:t;
y=1*(n==0); y=[zeros(1,t),ones(1,1),zeros(1,t)];
subplot(2,3,2); subplot(2,3,2);
stem(n,y); stem(n,y);
xlabel('t'); xlabel('t');
ylabel('x(t)'); ylabel('x(t)');
title('Unit Impulse'); title('Unit Impulse');
%Ramp Signal %Ramp Signal
n=0:1:t-1; n=0:1:t-1;
subplot(2,3,3); subplot(2,3,3);
stem(n,n); stem(n,n);
xlabel('t'); xlabel('t');
ylabel('x(t)'); ylabel('x(t)');
title('Ramp Signal'); title('Ramp Signal');
%Exponential signal %Exponential signal
n=0:1:t-1; n=0:1:t-1;
z=(a.^n); z=exp(a*n);
subplot(2,3,4); subplot(2,3,4);
stem(n,z); stem(n,z);
xlabel('t'); xlabel('t');
ylabel('x(t)'); ylabel('x(t)');
title('Exponential signal'); title('Exponential signal');
time=input('Enter The Time='); time=input('Enter The Time=');
%Sinusoidal Signal %Sinusoidal Signal
n=0:0.1:time; n=0:0.1:time;
f=0.2; f=0.2;
x=sin(2*pi*f*n); x=sin(2*pi*f*n);
subplot(2,3,5); subplot(2,3,5);
stem(n,x); stem(n,x);
xlabel('t'); xlabel('t');
ylabel('x(t)'); ylabel('x(t)');
title('Sinusoidal Signal'); title('Sinusoidal Signal');
%Cosine Signal %Cosine Signal
n=0:0.1:time; n=0:0.1:time;
f=0.2; f=0.2;
x=cos(2*pi*f*n); x=cos(2*pi*f*n);
subplot(2,3,6); subplot(2,3,6);
stem(n,x); stem(n,x);
xlabel('t'); xlabel('t');
ylabel('x(t)'); ylabel('x(t)');
title('Cosine Signal') title('Cosine Signal');
Linear and Circular Convolution without Conv function
close all;
clear all;
clc;
x=input('Enter the sequence 1:');
h=input('Enter the sequence 2:');
a=length(x);
b=length(h);
n=a+b-1;
m=max(a,b);
y=zeros(1,n);
z=zeros(1,m);
x1=[x zeros(1,m-a)];
h1=[h zeros(1,m-b)];
l=1:n;
for i=0:n %Linear Convolution Loop
for j=0:n
if ((i-j+1)>0 && (i-j+1)<=b && (j+1)<=a)
y(i+1)=y(i+1)+x(j+1).*h(i-j+1);
end
end
end
disp('Linear convolution sequence');
disp(y);
for i=0:m-1;%Circular Convolution loop
for j=0:m-1;
k=mod((i-j),m);
z(i+1)=z(i+1)+x1(j+1)*h1(k+1);
end
end
disp('Circular convolution sequence');
disp(z);

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

You might also like