0% found this document useful (0 votes)
6 views35 pages

DSP Programs

The document contains MATLAB programs for various signal processing tasks including Discrete Time Fourier Transform (DTFT), Discrete Fourier Transform (DFT), and DTMF signal generation and detection. It also compares the computation time of DFT using direct methods versus FFT, demonstrates linear filtering using DFT, and provides spectral analysis for single and dual tone signals. Additionally, it includes a program for estimating the periodicity of noise in periodic signals.

Uploaded by

professor240305
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)
6 views35 pages

DSP Programs

The document contains MATLAB programs for various signal processing tasks including Discrete Time Fourier Transform (DTFT), Discrete Fourier Transform (DFT), and DTMF signal generation and detection. It also compares the computation time of DFT using direct methods versus FFT, demonstrates linear filtering using DFT, and provides spectral analysis for single and dual tone signals. Additionally, it includes a program for estimating the periodicity of noise in periodic signals.

Uploaded by

professor240305
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/ 35

1 .

Discrete Time Fourier Transform [DTFT]

clc; clear; close all;


x=input('Enter the input sequence x[n]')
L=length(x);
n=0:1:L-1;
subplot(3,1,1);stem(n,x);title('Discrete time signal'); xlabel('n');ylabel('x[n]');legend('1SI23ET422');

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

Enter the input sequence x[n] = [ 3 2 2 3]


Enter the input sequence x[n] = [ 0 1 0 -1]

2 . Discrete Fourier Transform

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)

%magnitude and phase


Xm=abs(X);
Xp=angle(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

Enter the input sequence x[n] = [ 3 2 2 3]


Enter N-point dft = 4

10.0000 + 0.0000i 1.0000 - 1.0000i 0.0000 + 0.0000i 1.0000 + 1.0000i


Enter the input sequence x[n] = [ 3 2 2 3]
Enter N-point dft = 512

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

disp('The time taken by N-point DFT by FFT algorithm is')


tic
X=fft(x,N);
toc
Enter the input sequence x[n] = [ 3 2 2 3]

Enter N-point dft = 4


The time taken by N-point DFT by Direct Method is
Elapsed time is 0.003518 seconds.
The time taken by N-point DFT by FFT algorithm is
Elapsed time is 0.233898 seconds.

4 . Write a matlab program to verify linear filtering application using DFT

%% This is for the Linear Filtering


clc;clear;close all
x=input('Enter the input sequence x[n]')
h=input('Enter the impulse response h[n]')
L1=length(x)
L2=length(h)
N=L1+L2-1
X=fft(x,N)
H=fft(h,N)
Y=X.*H
y=ifft(Y,N)
N1=0:1:L1-1;
subplot(3,1,1),stem(N1,x);xlabel('n');ylabel('x[n]');title('Input Sequence x[n]');legend('1SI23ET422');
N2=0:1:L2-1;
subplot(3,1,2),stem(N2,h);xlabel('n');ylabel('h[n]');title('System h[n]'); legend('1SI23ET422');
N3=0:1:N-1;
subplot(3,1,3),stem(N3,y);xlabel('n');ylabel('y[n]');title('Linear Filtered output y[n]=x[n]*h[n]')
legend('1SI23ET422');

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

6 . Write a Matlab code to generate DTMF Signal

clc;clear all;close all;

% To define the lower and higher frequencies of telephone keypad


low_freqs = [697,770,852,941]
high_freqs = [1209, 1336, 1477, 1633]

% To display telephone keypad


disp('the telephone keypad matrix is')
disp('1 2 3 A');
disp('4 5 6 B');
disp('7 8 9 C');
disp('* 0 # D');

% To select the lower and higher frequencies of pressed key


key = input('enter any key from the above given set\n','s')
switch(key)
case '1'
fl = low_freqs(1);
fh = high_freqs(1);
case '2'
fl = low_freqs(1);
fh = high_freqs(2);
case '3'
fl = low_freqs(1);
fh = high_freqs(3);
case 'A'
fl = low_freqs(1);
fh = high_freqs(4);
case '4'
fl = low_freqs(2);
fh = high_freqs(1);
case '5'
fl = low_freqs(2);
fh = high_freqs(2);
case '6'
fl = low_freqs(2);
fh = high_freqs(3);
case 'B'
fl = low_freqs(2);
fh = high_freqs(4);
case '7'
fl = low_freqs(3);
fh = high_freqs(1);
case '8'
fl = low_freqs(3);
fh = high_freqs(2);
case '9'
fl = low_freqs(3);
fh = high_freqs(3);
case 'C'
fl = low_freqs(3);
fh = high_freqs(4);
case '*'
fl = low_freqs(4);
fh = high_freqs(1);
case '0'
fl = low_freqs(4);
fh = high_freqs(2);
case '#'
fl = low_freqs(4);
fh = high_freqs(3);
case 'D'
fl = low_freqs(4);
fh = high_freqs(4);
end

% To generate DTMF signal of pressed key


fs = 8000;
ts=1/fs;
cycles=10;
t=0:ts:cycles/fl;
x = 0.8*sin(2*pi*fl*t)+0.8*sin(2*pi*fh*t);
L=length(x);

% To plot DTMF signal of pressed key and its spectrum


subplot(221);plot(t,x);legend('1SI22ET014')
xlabel('t');ylabel('x(t)');title('DTMF signal');grid on;axis;
N=1024;
tic
Xm=abs(fft(x,N));
toc
f=0:fs/N:fs*(N-1)/N;
k=0:N-1;
subplot(222);plot(f(1:N/2),Xm(1:N/2));grid on;axis;%plot(f,Xm);
xlabel('f in Hz');ylabel('|X(f)|');title('Spectrum of DTMF signal using DFT');grid on;axis;legend('1SI23ET422')
fprintf('The lower and higher frequencies of pressed key are %d and %d\n',fl,fh);legend('1SI23ET422')
subplot(223);stem(k(1:N/2),Xm(1:N/2));grid on;axis;legend('1SI23ET422')%plot(f,Xm);
xlabel('k');ylabel('|X(k)|');title('Spectrum of DTMF signal using DFT');grid on;axis;
7. %% DTMF detection using Goertzel algorithm

clc;clear all;close all;

% To define the lower and higher frequencies of telephone keypad


low_freqs = [697,770,852,941]
high_freqs = [1209, 1336, 1477, 1633]

% To display telephone keypad


disp('the telephone keypad matrix is')
disp('1 2 3 A');
disp('4 5 6 B');
disp('7 8 9 C');
disp('* 0 # D');

% To select the lower and higher frequencies of pressed key


key = input('enter any key from the above given set\n','s')
switch(key)
case '1'
fl = low_freqs(1);
fh = high_freqs(1);
case '2'
fl = low_freqs(1);
fh = high_freqs(2);
case '3'
fl = low_freqs(1);
fh = high_freqs(3);
case 'A'
fl = low_freqs(1);
fh = high_freqs(4);
case '4'
fl = low_freqs(2);
fh = high_freqs(1);
case '5'
fl = low_freqs(2);
fh = high_freqs(2);
case '6'
fl = low_freqs(2);
fh = high_freqs(3);
case 'B'
fl = low_freqs(2);
fh = high_freqs(4);
case '7'
fl = low_freqs(3);
fh = high_freqs(1);
case '8'
fl = low_freqs(3);
fh = high_freqs(2);
case '9'
fl = low_freqs(3);
fh = high_freqs(3);
case 'C'
fl = low_freqs(3);
fh = high_freqs(4);
case '*'
fl = low_freqs(4);
fh = high_freqs(1);
case '0'
fl = low_freqs(4);
fh = high_freqs(2);
case '#'
fl = low_freqs(4);
fh = high_freqs(3);
case 'D'
fl = low_freqs(4);
fh = high_freqs(4);
end

% To generate DTMF signal of pressed key


fs = 8000;
ts=1/fs;
cycles=10;
t=0:ts:cycles/fl;
x = 0.8*sin(2*pi*fl*t)+0.8*sin(2*pi*fh*t);
L=length(x);

% To plot DTMF signal of pressed key and its spectrum


subplot(221);plot(t,x);legend('1SI23ET422')
xlabel('t');ylabel('x(t)');title('DTMF signal');grid on;axis;
N=1024;
tic
Xm=abs(fft(x,N));
toc
f=0:fs/N:fs*(N-1)/N;
k=0:N-1;
subplot(222);plot(f(1:N/2),Xm(1:N/2));grid on;axis;%plot(f,Xm);
xlabel('f in Hz');ylabel('|X(f)|');title('Spectrum of DTMF signal using DFT');grid on;axis;legend('1SI23ET422')
fprintf('The lower and higher frequencies of pressed key are %d and %d\n',fl,fh);legend('1SI23ET422')
subplot(223);stem(k(1:N/2),Xm(1:N/2));grid on;axis;legend('1SI23ET422')%plot(f,Xm);
xlabel('k');ylabel('|X(k)|');title('Spectrum of DTMF signal using DFT');grid on;axis;

%% DTMF detection using Goertzel algorithm

f = [697 770 852 941 1209 1336 1477 1633];


k = round((f/fs)*N)
L = length(x);
x = [x, zeros(1,N-L)];
tic

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

clc;clear all;close all;

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

fp=input('enter the pass band edge frequency in Hz');

fs=input('enter the stop band edge frequency in Hz');

Fs=input('enter the sampling frequency in Hz');

ks=input('enter the stopband attenuation in dB');

k=input('enter the value of k depending on window type');

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'

% Impulse response h[n]- symmetric about mid point alpha=(N-1)/2

figure(1);

subplot(311);stem(n,hd);xlabel('n'); ylabel('hd[n]');

title('ideal impulse response hd[n]');

subplot(312);stem(n,win);xlabel('n'); ylabel('w[n]');

title('window function w[n]');

subplot(313);stem(n,h);xlabel('n'); ylabel('h[n]');

title('impulse response h[n]');

% Frequency response of FIR LPF in dB

w = 0:0.0001:pi;

H = freqz(h,1,w);

figure(2);

subplot(211);plot(w,20*log10(abs(H)));grid on

xlabel('w in radians'); ylabel('G(ejw)');

title('magnitude response of FIR LPF in dB')

subplot(212);plot(w,unwrap(angle(H)));grid on

xlabel('w in radians'); ylabel('angle[H(ejw)]');

title('phase response of FIR LPF');


FIR in Radians

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

% Calculation of order and cutoff frequency


dw = ws-wp;
N = ceil(2*pi*k/dw);
if(rem(N,2)==0)
N=N+1
end

wc=wp+dw/2

% Impulse response of the filter

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'

% Impulse response h[n]- symmetric about mid point alpha=(N-1)/2


figure(1);
subplot(311);stem(n,hd);xlabel('n'); ylabel('hd[n]');
title('ideal impulse response hd[n]');
subplot(312);stem(n,win);xlabel('n'); ylabel('w[n]');
title('window function w[n]');
subplot(313);stem(n,h);xlabel('n'); ylabel('h[n]');
title('impulse response h[n]');

% Frequency response of FIR LPF in dB


w = 0:0.0001:pi;
H = freqz(h,1,w);
figure(2);
subplot(211);plot(w,20*log10(abs(H)));grid on
xlabel('w in radians'); ylabel('G(ejw)');
title('magnitude response of FIR LPF in dB')
subplot(212);plot(w,unwrap(angle(H)));grid on
xlabel('w in radians'); ylabel('angle[H(ejw)]');
title('phase response of FIR LPF');

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

% Calculation of order and cutoff frequency


dw = ws-wp;
N = ceil(2*pi*k/dw);
if(rem(N,2)==0)
N=N+1
end

wc=wp+dw/2

% Impulse response of the filter

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'

% Impulse response h[n]- symmetric about mid point alpha=(N-1)/2


figure(1);
subplot(311);stem(n,hd);xlabel('n'); ylabel('hd[n]');
title('ideal impulse response hd[n]');
subplot(312);stem(n,win);xlabel('n'); ylabel('w[n]');
title('window function w[n]');
subplot(313);stem(n,h);xlabel('n'); ylabel('h[n]');
title('impulse response h[n]');

% Frequency response of FIR LPF in dB


w = 0:0.0001:pi;
H = freqz(h,1,w);
figure(2);
subplot(211);plot(w,20*log10(abs(H)));grid on
xlabel('w in radians'); ylabel('G(ejw)');
title('magnitude response of FIR LPF in dB')
subplot(212);plot(w,unwrap(angle(H)));grid on
xlabel('w in radians'); ylabel('angle[H(ejw)]');
title('phase response of FIR LPF');

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

%b=input('enter the numz of H(z)'); % [0.2888 0.5775 0.2888]


%a=input('enter the denz of H(z)'); % [1.0000 -0.0166 0.1716]
h=[-1.55926873300775e-18 -0.00188521727268919 2.24174925616598e-18 0.00385703381668363
-4.18528935269118e-18 -0.00823084814773127 7.09400266112223e-18 0.0159246351743168
-1.05250639478023e-17 -0.0286361372156633 1.39561252344824e-17 0.0506470694906900
-1.68648385429134e-17 -0.0978777458205356 1.88083786394386e-17 0.315496417988156
0.500000000000000 0.315496417988156 1.88083786394386e-17 -0.0978777458205356
-1.68648385429134e-17 0.0506470694906900 1.39561252344824e-17 -0.0286361372156633
-1.05250639478023e-17 0.0159246351743168 7.09400266112223e-18 -0.00823084814773127
-4.18528935269118e-18 0.00385703381668363 2.24174925616598e-18 -0.00188521727268919
-1.55926873300775e-18];
y=filter(h,1,x);

% To plot the signal in time domain domain


figure(1)
subplot(2,1,1);plot(t,x);xlabel('t');ylabel('x(t)');
title('input signal to the filter');

subplot(2,1,2);plot(t,y);xlabel('t');ylabel('y(t)');
title('output of the filter');

% To plot the signal in frequency domain


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

%b=input('enter the numz of H(z)'); % [0.2888 0.5775 0.2888]


%a=input('enter the denz of H(z)'); % [1.0000 -0.0166 0.1716]
h=[-1.55926873300775e-18 -0.00188521727268919 2.24174925616598e-18 0.00385703381668363
-4.18528935269118e-18 -0.00823084814773127 7.09400266112223e-18 0.0159246351743168
-1.05250639478023e-17 -0.0286361372156633 1.39561252344824e-17 0.0506470694906900
-1.68648385429134e-17 -0.0978777458205356 1.88083786394386e-17 0.315496417988156
0.500000000000000 0.315496417988156 1.88083786394386e-17 -0.0978777458205356
-1.68648385429134e-17 0.0506470694906900 1.39561252344824e-17 -0.0286361372156633
-1.05250639478023e-17 0.0159246351743168 7.09400266112223e-18 -0.00823084814773127
-4.18528935269118e-18 0.00385703381668363 2.24174925616598e-18 -0.00188521727268919
-1.55926873300775e-18];
y=filter(h,1,x);

% To plot the signal in time domain domain


figure(1)
subplot(2,1,1);plot(t,x);xlabel('t');ylabel('x(t)');
title('input signal to the filter');

subplot(2,1,2);plot(t,y);xlabel('t');ylabel('y(t)');
title('output of the filter');

% To plot the signal in frequency domain

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

clc;clear all;close all;

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 ;

You might also like