Lab Report
Lab-6
Digital Signal Processing
Md. Zahidul Islam
160041010
Group: X
Moving Average Filter (MAF)
Task 1:
I have created a function maf(x,M) which is a moving average filter. I
have taken a signal x applied moving average filters of various sizes
using the function.
CODE:
x = zeros(1,500);
x(1,200:300) = 1;
noise = rand(size(x));
xnoise = x + noise;
y1 = maf(x,3);
y2 = maf(x,11);
y3 = maf(x,31);
y4 = maf(x,51);
figure;
subplot(3,2,1);plot(x); title("original signal");
subplot(3,2,2);plot(xnoise); title("signal with noise");
subplot(3,2,3);plot(y1); title("filtered signal with M=3");
subplot(3,2,4);plot(y2); title("filtered signal with M=11");
subplot(3,2,5);plot(y3); title("filtered signal with M=31");
subplot(3,2,6);plot(y4); title("filtered signal with M=51");
function [y] = maf(x,M)
n = size(x,2);
y = zeros(1,n);
p = (M-1)/2;
q = p+1;
y_1 = 0;
x_i_p = 0;
x_i_q = 0;
for i = 1:n
if i>1
y_1 = y(1,i-1);
end
if i+p<=n
x_i_p = x(1,i+p);
end
if i-q>=1
x_i_q = x(1,i-q);
end
y(1,i) = y_1 + x_i_p - x_i_q;
end
y = y/M;
end
Fig: moving average filter
A moving average filter with bigger kernel does more smoothing than
the one with the smaller kernel.
Task 2:
I have generated frequency response of moving average filters of
various sizes by the function “freqResMAF()”.
CODE:
y3 = freqResMAF(3);
y11 = freqResMAF(11);
y31 = freqResMAF(31);
y51 = freqResMAF(51);
figure;
plot(y3);
hold on;
plot(y11);
hold on;
plot(y31);
hold on;
plot(y51);
function [y]= freqResMAF(M)
x = linspace(0,.5,200);
y = zeros(size(x));
for i =1:size(x,2)
if x(1,i)~=0
y(1,i) = abs(sin(pi*x(1,i)*M) / (M*sin(pi*x(1,i)))) ;
end
end
end
Fig: frequency response of MAF.
The frequency response of a moving average filter of higher length has
faster roll off and more stopband attenuation.
Task 3:
using a 4x4 subplot. With a suitable MAF kernel, I used convolution to generate 2 pass and 4 pass
kernels.
I have also shown their step responses and Frequency responses in dB.
CODE:
x = zeros(1,24);
x(1,8:16) = 1;
subplot(2,2,1);
y1 = maf(x,9); y2= maf(y1,9);
plot(x,'.-'); hold on;
plot(y1, '.-'); hold on;
plot(y2, '.-'); title("Filter kernel");
x_freq = linspace(0,.5,200);
Y1 = freqResMAF(9);
Y2 = Y1 .* Y1;
Y3 = Y2 .* Y1;
subplot(2,2,2);
plot(x_freq,Y1,'.-'); hold on;
plot(x_freq,Y2, '.-'); hold on;
plot(x_freq,Y3, '.-'); title("Frequency Response");
step_y1 = cumsum(y1);
step_y2 = cumsum(y2);
step_y3 = cumsum(y3);
subplot(2,2,3);
plot(step_y1,'.-'); hold on;
plot(step_y2,'.-'); hold on;
plot(step_y3,'.-'); title("Step Response");
log_Y1 = 20*log(Y1);
log_Y2 = 20*log(Y2);
log_Y3 = 20*log(Y3);
subplot(2,2,4);
plot(x_freq, log_Y1,'.-'); hold on;
plot(x_freq, log_Y2,'.-'); hold on;
plot(x_freq, log_Y3,'.-'); title("Step Response");
Fig : analysis of one pass two pass and four pass MAF kernels.
We can see that, applying the moving average filter multiple times
increases the smoothing or averaging. The stopband attenuation
increases.
Windowed Sinc Filter
Task 4:
I wrote a sinc function and generated a signal x. Then I applied sinc
on x and got y. I plotted x and y.
CODE:
x = linspace(-50,50);
y = sinc(x, .1);
figure;
subplot(1,2,1);plot(x);
subplot(1,2,2);plot(y);
function [y] = sinc(x,f)
y = zeros(size(x));
for i =1:size(x,2)
if x(1,i)~=0
y(1,i) = sin(2*pi*f*x(1,i)) / (x(1,i)*pi ) ;
end
end
end
Sinc function is generated using the formula of sinc function given in
the text book. Sinc function is a good low pass filter.
Fig : sinc function.
Task 5:
I took the sinc function. And truncated it to the length M. Then I
smoothed it out using the hamming() function. It could have been done
using hamming too.
CODE:
x = linspace(-50,50);
f = .1;
y = sinc(x,f);
%y =[y_(1,:), zeros(1,50)];
Y = abs(fft(y));
x_freq = linspace(0,.5,50);
figure;
subplot(3,2,1);plot(y);
subplot(3,2,2);plot(x_freq,Y(1,1:50));
M = 40;
y_trunc = y;
y_trunc(1,70:100) = 0;
y_trunc = [y_trunc(1,30:100),zeros(1,20)];
Y_trunc = abs(fft(y_trunc));
subplot(3,2,3);plot(y_trunc);
subplot(3,2,4);plot(x_freq,Y_trunc(1,1:50));
y_trunc_hamming = conv(y_trunc, hamming(10));
Y_trunc_hamming = abs(fft(y_trunc_hamming));
subplot(3,2,5);plot(y_trunc_hamming);
subplot(3,2,6);plot(x_freq,Y_trunc_hamming(1,1:50));
Fig : smoothing sinc with hamming window.
We can see that hamming window smooths out the truncated sinc function
quite well. In frequency domain too, the smoothing happens.
Task 6:
Here I verified the characteristics of blackman and hamming window.
CODE:
b = blackman(50);
h = hamming(50);
x = linspace(-50,50);
f = .1;
y = sinc(x,f);
y_h = conv(y, h);
y_b = conv(y,b);
B = abs(fft(y_b));
H = abs(fft(y_h)) ;
x_freq = linspace(0,.5,50);
subplot(2,2,1);plot(h); hold on; plot(b); legend('hamming', 'blackman');
subplot(2,2,2);plot(y_h); hold on; plot(y_b);title('multiplication with
hamming and blackman');
subplot(2,2,3);plot(x_freq,H(1,1:50)); title('frequency response of
hamming');
subplot(2,2,4);plot(x_freq,B(1,1:50)); title('frequency response of
blackman');
Fig : smoothing sinc with hamming window.
We can see, Hamming window has faster roll-off than the Blackman.
Blackman has a better stopband attenuation. the Blackman has a passband
ripple of only about 0.02%, while the Hamming is typically 0.2%. In
general, the Blackman should be our first choice; a slow roll-off is
easier to handle than poor stopband attenuation.
Designing a band-pass filter
Task 7:
I have the built in functions butter() to make high pass and low pass
filters. Using them we can find the required band pass filter.
Code:
%designing the filter using a higher order filter
[b1, a1] = butter(25,1960/10000,'low');
[b2, a2] = butter(25,2040/10000, 'high');
H1 = abs(freqz(b1,a1,10000));
H2 = abs(freqz(b2,a2,10000));
H3 =( H1.*H2 )/.25;
x_freq = linspace(0,.5,5000);
figure;
subplot(3,1,1);plot(x_freq, H1(1:5000),'g');
subplot(3,1,2);plot( x_freq, H2(1:5000),'r');
subplot(3,1,3);plot( x_freq, H3(1:5000),'b' );
Fig: designing a bandpass filter.
_________________X__________________