Digital Signal Processing: Lab Task 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Alok Awasthi

17BEC0789
Faculty – Prof. Prakasam P.

Digital Signal Processing

Lab Task 3
Q1. Write a Matlab Program to compute the Discrete Fourier Transform without using inbuilt
function. Also check the same using in built fft function. Also plot its spectrum.

Objective: To compute the dft using MATLAB.

Algorithm:

1. Start
2. Get the values of sequence and length of the DFT
3. Calculate ‘w’ is exp(-i*2*pi/N)
4. Initialize nk = n*k
5. Initialize W=w.^nk
6. Initialize X=x*w
7. Plot ‘X’
8. Plot Magnitude and Phase Spectrum.
9. End

Program:
clc
clear all
close all
x=input('enter the value of x');
N=input('Enter the value of N');
len=length(x); if(N>len)
x=[x zeros(1,N-len)];
end
X=[];
syms s;
for i=(0:N-1)
s=0;
for n=(0:N-1)
s=s+(x(n+1)*exp(-1j*2*pi*i*n/N));
end
X(i+1)=s;
end
Y=fft(x,N)
k=0:N-1;
subplot(2,2,1)
stem(k,abs(X))
title('Magnitude Spectrum');
xlabel('n'); ylabel('Amplitude'); grid on;
subplot(2,2,2)
stem(k,atan(imag(X)./real(X)));
title('Phase Spectrum'); xlabel('n'); ylabel('Phase angle'); grid on;
subplot(2,2,3)
stem(k,Y);
title('Magnitude Spectrum'); xlabel('n'); ylabel('Amplitude'); grid on;
subplot(2,2,4)
stem(k,atan(imag(Y)./real(Y)));
title('Phase Spectrum'); xlabel('n'); ylabel('Phase angle'); grid on;

Manual Calculation:
Experimental Result:

Magnitude and Phase Spectrum

enter the value of x


[1 1 0 0]
Enter the value of N
4

Y =

2.0000 + 0.0000i 1.0000 - 1.0000i 0.0000 + 0.0000i 1.0000 + 1.0000i


Verification:

Inference: We were able to find the DFT with and without using inbuilt function and it came out
to be the same.
Q2. Write a Matlab Program to generate 10 Hz, 30 Hz and 50 Hz sinusoidal signals. Add all the
signals and generate the spectrum.
i. Design a low pass Butterworth Filter to pass only 10 Hz signal. Plot the magnitude
spectrum of the signal obtained at the output of the filter
ii. Design a high pass Butterworth Filter to pass only 50 Hz signal. Plot the magnitude
spectrum of the signal obtained at the output of the filter
iii. Design a band pass Butterworth Filter to pass only 30 Hz signal. Plot the magnitude
spectrum of the signal obtained at the output of the filter

Objective: To compute the summation of the signals and design all the three types of filters.

Algorithm:

1. Start
2. Calculate the summation of the 3 signals
3. Plot the 3 three signals using 16 point
4. Design Low Pass Filter, High Pass Filter and Band Pass Filter
5. Plot the Frequency response and the spectrum of all the three.
6. End

Program:

clc;
close all;
%Generation and addition of sinusoidal signal with 10Hz, 30Hz and 50 Hz
fs=150;
t = [0:0.1:2*pi]
x = sin(2*pi*10*t)+sin(2*pi*30*t)+sin(2*pi*50*t)
subplot(2,1,1);
plot(t,x); grid on; title('Input signal');
% Plotting the spectrum of the input signal
N=16
y=abs(fft(x,N));
f = linspace(-fs/2,fs/2,N);
% for double sided spectrum
subplot(2,1,2);
plot(f,y);grid on; title('Spectrum of input signal');

figure
% Design of Butterworth Low pass filter
fp=5; fs=25, ap=.5; as=25;
wp=2*pi*fp;
ws=2*pi*fs;
[N, wn]=buttord(wp, ws, ap, as, 's')
[b, a]=butter(N,wn, 's')
w=0:(3*ws)/511:3*ws;
h=freqs(b,a,w);
subplot(2,1,1);
plot(w/(2*pi), 20*log10(abs(h)));
grid on; title('Frequency Response of the Low Pass filter');
xlabel('Frequency in Hz');
ylabel('Gain in dB');
% Low pass filtering of the input signal
out = filter(b,a,x)
N=16;
z=abs(fft(out,N));
f = linspace(-fs/2,fs/2,N);
% for double sided spectrum
subplot(2,1,2);
plot(f,z);
grid on;
title('Spectrum of the 10Hz output Signal');

% Design of Butterworth High pass filter


figure
fp=50; fs=25, ap=.5; as=25
wp=2*pi*fp;
ws=2*pi*fs;
[N, wn]=buttord(wp, ws, ap, as, 's')
[b, a]=butter(N,wn,'high','s')
w=0:(3*ws)/511:3*ws;
h=freqs(b,a,w);
subplot(2,1,1);
plot(w/(2*pi), 20*log10(abs(h)));
grid on; title('Frequency Response of the High Pass filter');
xlabel('Frequency in Hz'); ylabel('Gain in dB');
% High pass filtering of the input signal
out = filter(b,a,x)
N=16;
z=abs(fft(out,N));
f = linspace(-fs/2,fs/2,N);
% for double sided spectrum
subplot(2,1,2);
plot(f,z);
grid on; title('Spectrum of the 50Hz output Signal');

% Design of Butterworth Band pass filter


figure
fp=[20 40];
fs=[15 45];
ap=.5;
as=25
wp=2*pi*fp;
ws=2*pi*fs;
[N, wn]=buttord(wp, ws, ap, as, 's')
[b, a]=butter(N,wn,'bandpass','s')
w=0:(3*ws)/511:3*ws;
h=freqs(b,a,w);
subplot(2,1,1);
plot(w/(2*pi), 20*log10(abs(h)));
grid on;
title('Frequency Response of the Band Pass filter');
xlabel('Frequency in Hz'); ylabel('Gain in dB');
out = filter(b,a,x)
N=16;
z=abs(fft(out,N));
f = linspace(15,45,N);
% for double sided spectrum
subplot(2,1,2);
plot(f,z);grid on;
title('Spectrum of the 30Hz output Signal');

Experimental Result:

Spectrum of summation of signals


Low Pass Filter

High Pass Filter


Band Pass Filter

Inference: We were able to generate the summation of three signals and also design Low pass,
High pass and Band pass filters.

You might also like