DSP Lab With Theory III Year ECE B Section
DSP Lab With Theory III Year ECE B Section
Aim:
To generate sinusoidal signal using recursive difference equation.
Tools required:
Computer with MATLAB software.
Theory:
𝐹
Sinusoidal signals, 𝑦𝑠 𝑛 = sin 2𝜋𝑓𝑛 and 𝑦𝑐 𝑛 = cos 2𝜋𝑓𝑛 with normalized frequency 𝑓 = 𝐹 (𝐹 is
𝑠
analog frequency and 𝐹𝑠 is sampling frequency) can be generated with following difference equations,
𝑦𝑠 𝑛 = 𝑎𝑐 𝑦𝑠 𝑛 − 1 + 𝑎𝑠 𝑦𝑐 𝑛 − 1
; 𝑓𝑜𝑟, 𝑛 > 0
𝑦𝑐 𝑛 = 𝑎𝑐 𝑦𝑐 𝑛 − 1 − 𝑎𝑠 𝑦𝑠 𝑛 − 1
where,
𝑎𝑐 = cos 2𝜋𝑓 , 𝑎𝑠 = sin 2𝜋𝑓 , 𝑦𝑠 0 = 0 and 𝑦𝑐 0 = 1.
Algorithm:
Step (1): Input normalized frequency 𝒇 of sinusoidal signal and number of samples 𝑵 to generate.
Step (2): Compute constants 𝒂𝒄 , 𝒂𝒔 and define initial values of sinusoidal signal 𝒚𝒔 𝟎 , 𝒚𝒄 𝟎 .
Step (3): Generate 𝑁 samples of sinusoidal signal using above difference equation 𝒚𝒔 𝒏 and 𝒚𝒄 𝒏 .
Step (4): Plot the generated sinusoidal signal.
Program:
% sinusoidal signal generation
% yc: cos(2*pi*f*n)
% ys: sin(2*pi*f*n)
clc; clear all; close all;
N=32; % N, number of samples
f = 1/16; % f, normalised frequency (-1/2<f<1/2)
yc = zeros(1,N);
ys = zeros(1,N);
yc(1) = 1;
ys(1) = 0;
ac = cos(2*pi*f);
as = sin(2*pi*f);
for n = 2:N
yc(n) = ac*yc(n-1)-as*ys(n-1);
ys(n) = ac*ys(n-1)+as*yc(n-1);
end
subplot(2,1,1);
stem(0:N-1,yc);title('yc(n) = cos(2\pi fn)');
subplot(2,1,2);
stem(0:N-1,ys);title('ys(n) = sin(2\pi fn)');
Conclusion:
Thus sinusoidal signals are generated using difference equations.
2. To find DFT / IDFT of given DT Signal
Aim:
To compute DFT and IDFT for given discrete-time signal.
Tools required:
Computer with MATLAB software.
Theory:
𝑁-point DFT is defined as,
𝑁−1
𝑋 𝑘 = 𝑥 𝑛 𝑒 −𝑗 2𝜋𝑛𝑘 /𝑁 𝑘 = 0,1, … , 𝑁 − 1
𝑛=0
𝑁-point IDFT is defined as,
𝑁−1
1
𝑥 𝑛 = 𝑋 𝑘 𝑒 𝑗 2𝜋𝑛𝑘 /𝑁 𝑛 = 0,1, … , 𝑁 − 1
𝑁
𝑘=0
Algorithm:
Step (1): Input 𝑵 length of DFT and discrete time signal 𝒙𝑛 with length ≤ 𝑁.
Step (2): If 𝒙𝑛 length is < 𝑁, pad zeros with 𝒙𝑛 to convert it into sequence of length 𝑁.
Step (3): Define 𝑁 × 𝑁 transformation matrix 𝑾.
Step (4): Compute 𝑁-point of DFT 𝑿 𝒌 .
Step (5): Compute 𝑁-point of IDFT 𝒙 𝒏 .
Step (6): Compare result with MATLAB built-in function fft() and ifft().
Program:
clc; clear all; close all;
N = 4; % length of DFT
x = [1,2,3]; % input discrete-time signal with length <= N
n = 0:N-1;
k = 0:N-1;
if length(x)<N % convert input signal into sequence of length N
x = [x zeros(1,N-length(x))];
end
W = exp(-1i*2*pi/N);
nk = n'*k;
Wn = W.^nk; % N x N transformation matrix
Xk = Wn*x'; % Xk N-point DFT of x
xn = conj(Wn)*Xk/N; % xn N-point IDFT of Xk
disp([Xk fft(x,N).']); % Compare DFT result with MATLAB built-in function fft()
disp([xn ifft(Xk)]); % Compare IDFT result with MATLAB built-in function ifft()
Conclusion:
Thus 𝑁-point DFT and IDFT are computed and results are verified using MATLAB built-in function fft and
ifft.
3. Implementation of Decimation Process
Aim:
To implement decimation or down-sampling process.
Tools required:
Computer with MATLAB software.
Theory:
Decimation or down-sampling by a factor 𝐷 is the process of sampling a sequence 𝑥 𝑛 into 𝑦 𝑛
by keeping every 𝐷th sample of 𝑥 𝑛 and deleting the 𝐷 − 1 samples in between, decimated or down-
sampled sequence 𝑦 𝑛 is given by,
𝑦 𝑛 = 𝑥(𝑛𝐷), −∞ < 𝑛 < ∞
If 𝑋 𝐹 is the spectrum of 𝑥 𝑛 , then 𝑌 𝐹 the spectrum of down-sampled or decimated signal 𝑦 𝑛 is given
by,
𝐷−1
1 𝐹 − 𝑘𝐹𝑠
𝑌 𝐹 = 𝑋
𝐷 𝐷
𝑘=0
where,
𝐹𝑠 is the sampling interval of 𝑥 𝑛 and 𝐷 is the decimation or down-sampling factor.
To avoid aliasing after decimation process, the highest normalized frequency 𝑓𝑚𝑎𝑥 or 𝜔𝑚𝑎𝑥 in 𝑋 𝐹 (the
spectrum of input signal 𝑥 𝑛 ) must satisfy the following condition,
1 𝜋
𝑓𝑚𝑎𝑥 ≤ 𝑜𝑟 𝜔𝑚𝑎𝑥 ≤
2𝐷 𝐷
Algorithm:
Step (1): Input decimation factor 𝑫 and discrete-time signal 𝒙 𝒏 .
Step (2): Compute down-sampled signal 𝒚 𝒏 .
1 1
Step (3): Compute spectrum of 𝑥 𝑛 and 𝑦 𝑛 for − 2 ≤ 𝑓 ≤ 2.
Step (4): Plot the sequence 𝑥 𝑛 , 𝑦 𝑛 and its spectrum.
Program:
% downsampling or decimation
clc; clear all; close all;
n = 0:20;
x = cos(2*pi*n/10)+cos(4*pi*n/10); % input signal
D = 2; % decimation factor
temp = mod(n,D);
ind = find(temp==0);
y = x(ind); % downsampled signal with index m
m = n(ind)/D;
N = 201; % samples for computing spectrum
X = fftshift(abs(fft(x,N))); % spectrum of input x
Y = fftshift(abs(fft(y,N))); % spectrum of down-sampled signal y
f = linspace(-0.5,0.5,N); % normalized frequency
subplot(2,2,1);
stem(n,x);title('x(n)');
subplot(2,2,2);
plot(f,X);
title(' Magnitude spectrum of x(n)');
subplot(2,2,3);
stem(m,y);
title('y(n)-downsampled signal');
subplot(2,2,4);
plot(f,Y);
title('Magnitude spectrum of y(n)');
Conclusion:
Thus decimation or down-sampling process is implemented.
4. Implementation of Interpolation Process
Aim:
To implement interpolation or up-sampling process.
Tools required:
Computer with MATLAB software.
Theory:
Interpolation or up-sampling by a factor 𝐼 is the process of increasing sampling rate of a sequence
𝑥 𝑛 by 𝐼 by placing 𝐼 − 1 equally spaced zeros between each pair of samples. Interpolated or up-sampled
sequence 𝑦 𝑛 is given by,
𝑛
𝑥 , 𝑚 = 0, ±𝐼, ±2𝐼, …
𝑦 𝑛 = 𝐼
0, 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
If 𝑋 𝐹 is the spectrum of 𝑥 𝑛 , then 𝑌 𝐹 the spectrum of up-sampled or interpolated signal 𝑦 𝑛 is given
by,
𝑌 𝐹 =𝑋 𝐹𝐼
where,
𝐼 is the interpolation or up-sampling factor.
Algorithm:
Step (1): Input interpolation factor 𝑰 and discrete-time signal 𝒙 𝒏 .
Step (2): Compute up-sampled signal 𝒚 𝒏 .
1 1
Step (3): Compute spectrum of 𝑥 𝑛 and 𝑦 𝑛 for − 2 ≤ 𝑓 ≤ 2.
Step (4): Plot the sequence 𝑥 𝑛 , 𝑦 𝑛 and its spectrum.
Program:
% upsampling or Interpolation
clc; clear all; close all;
n = 0:20;
x = cos(2*pi*n/10)+cos(4*pi*n/10); % x - input signal
I = 2; % interpolation factor
m = I*n(1):I*n(end);
y = zeros(length(m),1); % upsampled signal with index m
for ix=0:(length(n)-1)
y(I*ix+1)=x(ix+1);
end
N = 201; % samples for computing spectrum
X = fftshift(abs(fft(x,N))); % spectrum of input x
Y = fftshift(abs(fft(y,N))); % spectrum of up-sampled signal y
f = linspace(-0.5,0.5,N); % normalized frequency
subplot(2,2,1);
stem(n,x);title('x(n)');
subplot(2,2,2);
plot(f,X);
title(' Magnitude spectrum of x(n)');
subplot(2,2,3);
stem(m,y);
title('y(n)-upsampled signal');
subplot(2,2,4);
plot(f,Y);
title('Magnitude spectrum of y(n)');
Conclusion:
Thus interpolation or up-sampling process is implemented.
5. Implementation of HP IIR Filter for a given Sequence/Signal
Aim:
To compare 𝑁th order high pass Butterworth and Chebyshev filter.
Tools required:
Computer with MATLAB software.
Theory:
𝑁th order digital high pass filter Butterworth filter 𝐻𝑏 𝑧 is given by,
𝐻𝑏 𝑧 = 𝐻𝑏 (𝑠) 2 1−𝑧 −1
𝑠=
𝑇 1+𝑧 −1
where, 𝐻𝑏 (𝑠) is analog transfer function of Butterworth filter given by,
1
𝐻𝑏 𝑠 =
𝑁−1 Ω𝑐
𝑘=0 𝑠 − 𝑝𝑘
where, Ω𝑐 is cutoff frequency in radians/sec, which is related to digital cutoff frequency ω𝑐 radians/sample
2 ω𝑐
is given by Ω𝑐 = 𝑇 tan and 𝑝𝑘 is pole locations of low pass butterworth filter with cutoff frequency 1
2
𝜋 2𝑘+1 𝜋
radians/sec, which is given by 𝑝𝑘 = 𝑒 𝑗 2 + 2𝑁 , 𝑘 = 0,1, … , 𝑁 − 1.
𝑁th order digital high pass filter Chebychev filter 𝐻𝑐 𝑧 is given by,
𝐻𝑐 𝑧 = 𝐻𝑐 (𝑠) 2 1−𝑧 −1
𝑠=
𝑇 1+𝑧 −1
where, 𝐻𝑐 (𝑠) is analog transfer function of Chebychev filter given by,
1
𝐻𝑐 𝑠 =
𝑁−1 Ω𝑝
𝑘=0 𝑠 − 𝑝𝑘
where, Ω𝑝 is pass band frequency in radians/sec, which is related to digital pass band frequency ω𝑝
2 ω𝑝
radians/sample is given by Ω𝑝 = 𝑇 tan and 𝑝𝑘 is pole locations of low pass Chebychev filter with pass
2
band frequency 1 radians/sec, which is given by
𝑝𝑘 = 𝑥𝑘 + 𝑗𝑦𝑘 , 𝑘 = 0,1, … , 𝑁 − 1,
𝜋 2𝑘+1 𝜋 𝛽 2 +1 𝛽 2 −1
in which 𝑥𝑘 = 𝑟2 cos 𝜙𝑘 , 𝑦𝑘 = 𝑟1 sin 𝜙𝑘 , where, 𝜙𝑘 = 2 + , 𝑟1 = , 𝑟2 = , and 𝛽 =
2𝑁 2𝛽 2𝛽
1/𝑁
1+𝜖 2 +1 1
where, is magnitude at pass band frequency ω𝑝 .
𝜖 1+𝜖 2
The magnitude response 𝐻(𝜔) of digital filter 𝐻(𝑧) is given by,
𝐻(𝜔) = 𝐻(𝑧) 𝑧=𝑒 𝑗𝜔 , 0 ≤ 𝜔 ≤ 𝜋.
The magnitude response of Butterworth filter has monotonic characteristics in both pass band and stop band,
Chebychev filter exhibit equiripple behavior in the pass band and a monotonic characteristic in stop band.
For same order of the filter, Butterworth filter has more transition band when compared to Chebychev filter.
Algorithm:
Step (1): Input pass band frequency ω𝑝 at 3-dB attenuation and order of the filter 𝑁.
Step (2): Compute digital filter coefficients of high pass Butterworth and Chebychev filter.
Step (3): Compute frequency response of digital Butterworth and Chebychev filter.
Step (4): Plot the magnitude response of digital Butterworth and Chebychev filter.
Program:
%IIR filters
clc; clear all; close all;
Wp = pi/2; % passband frequency in radians/sample
Rp = 3; % passband ripple in dB
N = 3; % order of the filter
[b, a] = butter(N,Wp/pi,'high'); % Nth order Butterworth filter
[Hb,W]= freqz(b,a); % frequency response Butterworth filter
[b1, a1] = cheby1(N,Rp,Wp/pi,'high'); % Nth order type I Chebyshev filter
Hc = freqz(b1,a1); % frequency response type I Chebyshev filter
plot(W/pi,abs(Hb),'-',W/pi,abs(Hc),'--');
legend('Butterworth filter','Type I Chebychev filter');
title('Comparison of Nth order Butterworth and Chebychev filter')
xlabel('\omega / \pi, normalized frequency in rad/sample');
ylabel('|H(\omega)|');
Conclusion:
Thus 𝑁th order high pass Butterworth and Chebyshev filter are compared by plotting magnitude response.
6. Implementation of LP FIR Filter for a given Sequence/Signal.
Aim:
To design low pass FIR filter using windowing techniques.
Tools required:
Computer with MATLAB software.
Theory:
Ideal frequency response of low pass filter with cutoff frequency 𝜔𝑐 is given by,
1, 𝜔 ≤ 𝜔𝑐
𝐻𝑑 𝜔 =
0, 𝜔𝑐 < 𝜔 < 𝜋
Corresponding impulse response ℎ𝑑 (𝑛) is obtained by inverse discrete Fourier transform given by,
𝜋
𝜔𝑐
1 ,𝑛 = 0
𝜋
ℎ𝑑 𝑛 = 𝐻𝑑 𝜔 𝑒 𝑗𝜔𝑛 𝑑𝜔 = ℎ𝑑 (𝑛) = sin(𝜔 𝑛)
2𝜋 𝑐
𝜔 =−𝜋 ,n ≠ 0
𝜋𝑛
Need of window function in FIR filter design:
Abrupt truncation of infinite impulse response to finite length (as in rectangular window) introduce
ripples in the frequency response characteristic 𝐻(𝜔). The oscillatory behavior near the band edge of the filter is
called the Gibbs phenomenon. To alleviate the presence of large oscillations in both the passband and the stopband,
we should use a window function that contains a taper and decays toward zero gradually, instead of abruptly, as it
occurs in a rectangular window. Various window function (including rectangular window) are listed below.
M−1 M−1
Window Window function, 𝑤 𝑛 , − ≤n≤
2 2
Rectangular 1
2|𝑛|
Bartlett or Triangular 1 −
𝑀−1
1 2𝜋𝑛
Hanning 1 + 𝑐𝑜𝑠
2 𝑀−1
2𝜋𝑛
Hamming 0.54 + 0.46𝑐𝑜𝑠
𝑀−1
2𝜋𝑛 4𝜋𝑛
Blackman 0.42 + 0.5𝑐𝑜𝑠 + 0.08𝑐𝑜𝑠
𝑀−1 𝑀−1
Impulse response of a Linear phase FIR filter using windowing technique is given by,
M−1
𝒉(𝒏) = 𝒉𝒅 (𝒏)𝒘(𝒏), 𝑓𝑜𝑟 − N ≤ n ≤ N, where, N = , 𝑀-is FIR filter length
2
Program:
%FIR filters
clc; clear all; close all;
M = 25; % filter length
wc = 0.5; % normalized cutoff frequency wc/pi
wr = rectwin(M); % rectangular window sequence
wh = hamming(M); % hamming window sequence
hr = fir1(M-1,wc,wr); % LPF with rectangular window
hh = fir1(M-1,wc,wh); % LPF with hamming window
[Hr,W] = freqz(hr,1);
[Hh,W] = freqz(hh,1);
plot(W/pi,abs(Hr),W/pi, '-',abs(Hh), '--')
legend('rectangular','Hamming');
title('FIR filter comparison')
xlabel('\omega, normalied frequency');
ylabel('|H(\omega)|');
Conclusion:
Thus low pass FIR filters are designed using windowing technique and filters are compared using magnitude
response.