DSP Programs
DSP Programs
w=0:0.01:2*pi;
for p=1:length(w)
X(p)=0;
for ni=1:length(x)
X(p) =X(p) + x(ni).*exp(-i.*w(p).*n(ni))
end
end
subplot(3,1,2);plot(w,abs(X));xlabel('w in rad');ylabel('|X(e^j^w)|');title('Frequency response of x[n] (DTFT
Magnitude Plot)');
legend('1SI23ET422');
subplot(3,1,3);plot(w,angle(X));xlabel('w in rad');ylabel('Phase of X(e^j^w)');title('Frequency response of x[n] (DTFT
Phase Plot)');
legend('1SI23ET422');
Output
clc;clear;close all;
x=input('enter the input sequence x[n]')
l=length(x);
N=input('enter N-point dft');
if(N>l)
x=[x,zeros(1,(N-l))]
end
%compution of dft
for k=0:1:N-1;
X(k+1)=0;
for n=0:1:N-1;
X(k+1)=X(k+1)+(x(n+1)*exp(1i*2*pi*k*n/N));
end
end
disp(X)
%IDFT COMPUTATION
for n=0:1:N-1;
xr(n+1)=0;
for k=0:1:N-1;
xr(n+1)=xr(n+1)+(X(k+1)*exp(-1i*2*pi*k*n/N));
end
end
xr=xr/N;
%plot of graph
k=0:1:N-1;
n=0:1:N-1;
subplot(411);stem(n,x);xlabel('n');ylabel('x[n]');title('original signal');
legend('1SI23ET422');
subplot(412);stem(k,Xm);xlabel('n');ylabel('Xm[k]');title('magnitudespect');
legend('1SI23ET422');
hold on
plot(k,Xm);
subplot(413);stem(k,Xp);xlabel('n');ylabel('Xp[k]');title('phase spect');
legend('1SI23ET422');
subplot(414);stem(n,xr);xlabel('n');ylabel('xr[n]');title('IDFT signal');
legend('1SI23ET422');
Output
3 . Write a Matlab program to compare the Time taken by discrete method of DFT Computation by FFT
algorithm
clc;clear;close all;
x=input('enter the input sequence x[n]')
l=length(x);
N=input('enter N-point dft');
if(N>l)
x=[x,zeros(1,(N-l))]
end
%compution of dft
disp('The time taken by N-point DFT by Direct Method is')
tic
for k=0:1:N-1;
X(k+1)=0;
for n=0:1:N-1;
X(k+1)=X(k+1)+(x(n+1)*exp(1i*2*pi*k*n/N));
end
end
toc
OUTPUT
5 . Write a matlab Code for Spectral analysis using DFT
Single Tone
clc;clear;close all;
A=5;
f1=1000;
f2=1500;
fs=8000;
ts=1/fs;
cycles=10;
t=0:ts:cycles/f1;
x=A*sin(2*pi*f1*t)
figure(1)
subplot(311);plot(t,x);
xlabel('n');ylabel('x[n]');hold on ;stem(t,x);grid on;title('Dualtone Analog siganl');legend('1SI23ET422')
L=length(x);
N=1024;
X=fft(x,N);
Xm=abs(X);
k=0:1:N-1;
fhz=0:fs/N:(N-1)*fs/N;
subplot(312);stem(k,Xm);
xlabel('k');ylabel('|X(k)|');title('Spectrum of Analog signal vs k');legend('1SI23ET422')
subplot(313);plot(fhz,Xm);
xlabel('f in Hz');ylabel('|X(f)|');title('Spectrum of Analog signal vs f');legend('1SI23ET422');
OUTPUT
Dual Tone
clc;clear;close all;
A=5;
f1=1000;
f2=1500;
fs=8000;
ts=1/fs;
cycles=10;
t=0:ts:cycles/f1;
x=A*sin(2*pi*f1*t)+A*sin(2*pi*f2*t);
figure(1)
subplot(311);plot(t,x);
xlabel('n');ylabel('x[n]');hold on ;stem(t,x);grid on;title('Dualtone Analog siganl');legend('1SI23ET422')
L=length(x);
N=1024;
X=fft(x,N);
Xm=abs(X);
k=0:1:N-1;
fhz=0:fs/N:(N-1)*fs/N;
subplot(312);stem(k,Xm);
xlabel('k');ylabel('|X(k)|');title('Spectrum of Analog signal vs k');legend('1SI23ET422')
subplot(313);plot(fhz,Xm);
xlabel('f in Hz');ylabel('|X(f)|');title('Spectrum of Analog signal vs f');legend('1SI23ET422');
for s = 1:8
Xg(s) = goertzel(x,k(s)+1);
end
toc
Xgm = abs(Xg);
subplot(224),stem(k,Xgm);grid on; xlabel('k');ylabel('|X[k]|');legend('1SI23ET422')
title(' DTMF signal detected using Goertzel ');grid on;axis;
OUTPUT
8 . Write a matlab Program to Estimate the Periodicity of Noise Periodic Signal
Single Tone
clc;clear;close all;
A=0.8;
f1=100;
f2=200;
fs=8000;
ts=1/fs;
cycles=5;
t=0:ts:cycles/f1;
x=A*sin(2*pi*f1*t);
L=length(x);
d=rand(1,L)-0.5;
y=x+d;
figure(1)
subplot(231);plot(t,x);hold on; stem(t,x)
xlabel('t');ylabel('x(t)');
title('periodic signal')
subplot(233);plot(t,y);xlabel('t');ylabel('y(t)');
title('periodic composite signal currupted by noise');
legend('1SI23ET422')
subplot(232);plot(t,d);
Rdd=xcorr(d);
l=-(length(Rdd)-1)/2:1:((length(Rdd)-1)/2);
subplot(235);plot(l,Rdd);
Rxx=xcorr(x);
l=-(length(Rxx)-1)/2:1:((length(Rxx)-1)/2);
subplot(234);plot(l,Rxx);
title('Auto correlation of x(t)');
xlabel('lag l');ylabel('Auto correlation');
Ryy=xcorr(y);
subplot(236);plot(l,Ryy);
title('Auto correlation of y(t)');
xlabel('lag 1');ylabel('Auto correlation');
legend('1SI23ET422')
Dual Tone
clc;clear;close all;
A=0.8;
f1=100;
f2=200;
fs=8000;
ts=1/fs;
cycles=5;
t=0:ts:cycles/f1;
x=A*sin(2*pi*f1*t)+A*cos(2*pi*f2*t);
L=length(x);
d=rand(1,L)-0.5;
y=x+d;
figure(1)
subplot(231);plot(t,x);hold on; stem(t,x)
xlabel('t');ylabel('x(t)');
title('periodic signal')
subplot(233);plot(t,y);xlabel('t');ylabel('y(t)');
title('periodic composite signal currupted by noise');
legend('1SI23ET422')
subplot(232);plot(t,d);
Rdd=xcorr(d);
l=-(length(Rdd)-1)/2:1:((length(Rdd)-1)/2);
subplot(235);plot(l,Rdd);
Rxx=xcorr(x);
l=-(length(Rxx)-1)/2:1:((length(Rxx)-1)/2);
subplot(234);plot(l,Rxx);
title('Auto correlation of x(t)');
xlabel('lag l');ylabel('Auto correlation');
Ryy=xcorr(y);
subplot(236);plot(l,Ryy);
title('Auto correlation of y(t)');
xlabel('lag 1');ylabel('Auto correlation');
legend('1SI23ET422')
9 . Windows Function
N=51;
n=0:1:N-1;
figure(1);
w=boxcar(N);
W=fft(w,512);
Wmag=abs(W);
gain=20*log10(Wmag);
subplot(211);stem(n,w);title("rectangular window");legend("1SI23ET422");ylabel("w(n)");xlabel("n")
subplot(212);plot(gain);ylabel("gain in db");xlabel("w in radians");title("frequency response of rectangular
window");
figure(2);
w=bartlett(N);
W=fft(w,512);
Wmag=abs(W);
gain=20*log10(Wmag);
subplot(211);stem(n,w);title("triangular window");legend("1SI23ET422");ylabel("w(n)");xlabel("n")
subplot(212);plot(gain);ylabel("gain in db");xlabel("w in radians");title("frequency response of triangular window");
figure(3);
w=hanning(N);
W=fft(w,512);
Wmag=abs(W);
gain=20*log10(Wmag);
subplot(211);stem(n,w);title("hanning window");legend("1SI23ET422");ylabel("w(n)");xlabel("n")
subplot(212);plot(gain);ylabel("gain in db");xlabel("w in radians");title("frequency response of hanning window");
figure(4);
w=hamming(N);
W=fft(w,512);
Wmag=abs(W);
gain=20*log10(Wmag);
subplot(211);stem(n,w);title("hamming window");legend("1SI23ET422");
subplot(212);plot(gain);ylabel("gain in db");xlabel("w in radians");title("frequency response of hamming window");
figure(5);
w=blackman(N);
W=fft(w,512);
Wmag=abs(W);
gain=20*log10(Wmag);
subplot(211);stem(n,w);title("blackman window");legend("1SI23ET422");
subplot(212);plot(gain);ylabel("gain in db");xlabel("w in radians");title("frequency response of blackman
window");
10. Design , Implement and Verify Digital FIR Low pass Filter for the given specifications.
Frequency in HZ
clc;clear;close all;
% Analog specifications in HZ
wp=2*pi*fp*1/Fs
ws=2*pi*fs*1/Fs
dw = ws-wp;
N = ceil(2*pi*k/dw);
if(rem(N,2)==0)
N=N+1
end
wc=wp+dw/2
alpha=(N-1)/2;
n=0:1:N-1;
hd=sin(wc*(n-alpha))./(pi*(n-alpha))
hd(alpha+1)=wc/pi
win=hamming(N) %boxcar(N),bartlett(N),hann(N),blackman(N)
h=hd.*win'
figure(1);
subplot(311);stem(n,hd);xlabel('n'); ylabel('hd[n]');
subplot(312);stem(n,win);xlabel('n'); ylabel('w[n]');
subplot(313);stem(n,h);xlabel('n'); ylabel('h[n]');
w = 0:0.0001:pi;
H = freqz(h,1,w);
figure(2);
subplot(211);plot(w,20*log10(abs(H)));grid on
subplot(212);plot(w,unwrap(angle(H)));grid on
clc;clear;close all;
wc=wp+dw/2
alpha=(N-1)/2;
n=0:1:N-1;
hd=sin(wc*(n-alpha))./(pi*(n-alpha))
hd(alpha+1)=wc/pi
win=hamming(N) %boxcar(N),bartlett(N),hann(N),blackman(N)
h=hd.*win'
figure(3)
zplane(h,1)
Change the window function to blackman
clc;clear;close all;
% Digital specifications in radians
wp=input('enter the passband edge frequency in radian')
ws=input('enter the stopband edge frequency in radian')
ks=input('enter the stopband attenuation in dB');
k=input('enter the value of k depending on window type');
wc=wp+dw/2
alpha=(N-1)/2;
n=0:1:N-1;
hd=sin(wc*(n-alpha))./(pi*(n-alpha))
hd(alpha+1)=wc/pi
win=blackman(N) %boxcar(N),bartlett(N),hann(N),blackman(N)
h=hd.*win'
figure(3)
zplane(h,1)
11 . Verification of FIR
clc;clear;close all;
A = 0.8;
f1 = 1000;
f2 = 3000;
Fs = 8000;
t = 0:1/Fs:10/f1;
x = A*sin(2*pi*f1*t)+A*sin(2*pi*f2*t);
subplot(2,1,2);plot(t,y);xlabel('t');ylabel('y(t)');
title('output of the filter');
figure(2);
subplot(2,1,1);plot(f,abs(X));xlabel('f in Hz');ylabel('|X(k)|');
title('frequencies of input signal to the filter')
subplot(2,1,2);plot(f,abs(Y));xlabel('f in Hz');ylabel('|Y(k)|');
title('frequency present in output signal after filtering')
Change the values
clc;clear;close all;
A = 0.8;
f1 = 1000;
f2 = 6000;
Fs = 11000;
t = 0:1/Fs:10/f1;
x = A*sin(2*pi*f1*t)+A*sin(2*pi*f2*t);
subplot(2,1,2);plot(t,y);xlabel('t');ylabel('y(t)');
title('output of the filter');
N=1024;
X=fft(x,N);
Y=fft(y,N);
f=0:Fs/N:(N-1)*Fs/N;
figure(2);
subplot(2,1,1);plot(f,abs(X));xlabel('f in Hz');ylabel('|X(k)|');
title('frequencies of input signal to the filter')
subplot(2,1,2);plot(f,abs(Y));xlabel('f in Hz');ylabel('|Y(k)|');
title('frequency present in output signal after filtering')
12. Hilbert Transform
N = 11;
alpha = (N-1)/2;
n = 0:1:N-1;
hd = (2*sin(pi*(n-alpha)/2).^2)./(pi*(n-alpha))
hd(alpha+1) = 0
win = hamming(N) % change the window function
h = hd.*win'
figure(1);
subplot(221); stem(n,hd);
xlabel('n'); ylabel('hd[n]');title('infinite impulse response')
subplot(222); stem(n,win);
xlabel('n'); ylabel('w[n]');title('finite and causal window')
subplot(223); stem(n,h);
xlabel('n'); ylabel('h[n]=hd[n]xw[n]');title('finite impulse response')
w = -pi:0.0001:pi;
H = freqz(h,1,w);
H_mag = abs(H);
subplot(224);plot(w,H_mag);
xlabel('w in radians'); ylabel('magnitude');
title('magnitude response of Hilbert transformer')
N=101;
13 . Differentiator
clc;clear;close all;
N = 7;
alpha = (N-1)/2;
n = 0:1:N-1;
hd = (cos(pi*(n-alpha)))./(n-alpha)
hd(alpha+1) = 0
win = hamming(N) % change the window function
h = hd.*win'
figure(1);
subplot(221); stem(n,hd);
xlabel('n'); ylabel('hd[n]');title('infinite impulse response')
subplot(222); stem(n,win);
xlabel('n'); ylabel('w[n]');title('finite and causal window')
subplot(223); stem(n,h);
xlabel('n'); ylabel('h[n]=hd[n]xw[n]');title('finite impulse response')
w = -pi:0.0001:pi;
H = freqz(h,1,w);
H_mag = abs(H);
subplot(224);plot(w,H_mag);
xlabel('w in radians'); ylabel('magnitude');title('magnitude response of differentiator')
N = 101 ;