0% found this document useful (0 votes)
11 views

dsp lab pdf

The document contains MATLAB programs for analyzing digital filters using various windowing techniques including Hamming, Hanning, Blackman, and Rectangular windows. Each program computes low-pass and high-pass FIR filter coefficients, displays their magnitude and phase responses, and verifies properties like linearity, circular time shift, frequency shift, and Parseval's property. The execution results demonstrate the coefficients and responses for specified cutoff frequencies and sampling rates.

Uploaded by

H M BRUNDA
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)
11 views

dsp lab pdf

The document contains MATLAB programs for analyzing digital filters using various windowing techniques including Hamming, Hanning, Blackman, and Rectangular windows. Each program computes low-pass and high-pass FIR filter coefficients, displays their magnitude and phase responses, and verifies properties like linearity, circular time shift, frequency shift, and Parseval's property. The execution results demonstrate the coefficients and responses for specified cutoff frequencies and sampling rates.

Uploaded by

H M BRUNDA
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/ 25

PROGRAM :

clc
clear all
close all
% a) LINEARITY PROPERTY
x1 = input( ' Enter the sequence x1[n] = ' );
x2 = input( ' Enter the sequence x2[n] = ' );
N1 = length(x1);
N2 = length(x2);
if ( N1 > N2 )
x2 = [ x2, zeros( 1, N1 - N2 ) ];
else
x1 = [ x1, zeros( 1, N2 - N1 ) ];
end
N = max( N1, N2 );
a1 = input('arbitrary constant value a1 = ');
a2 = input('arbitrary constant value a2 = ');
LHS = fft( a1*x1 + a2*x2 , N );
RHS = a1 * fft( x1, N ) + a2 * fft( x2, N );
disp( LHS )
disp( RHS )

% b) CIRCULAR TIME SHIFT PROPERTY


x = input(' Enter the sequence x[n] = ' );
N = length(x);
[ m ]= input('Shift value in integer = ' );
X = fft( x, N )
temp = circshift(x', m)'
disp('DFT of time shifted sequence ')
disp(' ' )
X1 = fft( temp, N )
k = 0:N-1;
X2 = exp( -i * 2 * pi * m * k / N ) .* X

% c) CIRCULAR FREQUENCY SHIFT PROPERTY


x = input(' Enter the sequence x[n] = ' )
N = length(x);
X = fft(x, N)
n = 0:N-1;
m = input(' Enter the shift parameter = ' )
y = exp(i*2*pi*m*n/N) .*x;
Y = fft(y, N)
Xfreq_shift = circshift( X', m )'
% d) Parseval's Property
clear all
x = input(' Enter the sequence x[n] = ' );
y = input(' Enter the sequence y[n] = ' );
Nx = length(x);
Ny = length(y);
if ( Nx>Ny )
y = [ y, zeros( 1, Nx - Ny ) ];
else
x = [ x, zeros( 1, Ny - Nx ) ];
end
N = max(Nx, Ny );
X = fft( x, N );
Y = fft( y, N );
LHS = sum( (x .* conj(y)) )
RHS = (1/N) * sum((X .* conj(Y)) )

AFTER THE EXECUTION:

1.Linearity properties:
Enter the sequence x1[n] = [1,0.54,-0.41,-0.98]
Enter the sequence x2[n] = [0,0.84,0.9,0.14]
arbitrary constant value a1 = 2
arbitrary constant value a2 = 3
5.9400 + 0.0000i 0.1200 - 5.1400i 1.8200 + 0.0000i 0.1200 + 5.1400i

5.9400 + 0.0000i 0.1200 - 5.1400i 1.8200 + 0.0000i 0.1200 + 5.1400i

2. circular time shift properties


Enter the sequence x[n] = [1,-1,1,-1]
Shift value in integer = 2

X= 0 0 4 0
temp = 1 -1 1 -1

DFT of time shifted sequence

X1 = 0 0 4 0

X2 =

0.0000 + 0.0000i 0.0000 + 0.0000i

4.0000 + 0.0000i 0.0000 + 0.0000i


3.Circular frequency shift

Enter the sequence x[n] = [1,0,1,0]

X= 2 0 2 0

Enter the shift parameter = 2

Y=
2.0000 - 0.0000i 0.0000 + 0.0000i

2.0000 - 0.0000i 0.0000 + 0.0000i

X freq_shift = 2 0 2 0

Enter the sequence x[n] = [1,0,1,0]

X=2 0 2 0

Enter the shift parameter = 2

Y=
2.0000 - 0.0000i 0.0000 + 0.0000i

2.0000 - 0.0000i 0.0000 + 0.0000i

Xfreq_shift = 2 0 2 0

4.parseval’s property

Enter the sequence x[n]=[2 1 2 1]

Enter the sequence y[n]=[1 2 3 4]

LHS=14

RHS=14

LHS=RHS
PROGRAM:
HAMMING WINDOW:
clc
clear all
close all
fc = input('Enter the cut off frequency in Hz:');
Fs = input('Enter the sampling frequency in Hz:');
N= input('Enter the order of the filter N=');
wc= ((2*pi*fc)/Fs);
window = hamming(N+1);
hn = fir1(N,wc/pi, window);
w=0:0.01:pi;
Hw= freqz(hn,1 ,w);
display('The LPF filter coefficients are');
display(hn);

subplot(2,2,1);
plot(w/pi,20*log(abs(Hw)));
grid;
xlabel('NormaIised frequency');
ylabel('Magnitude');
title( 'Magnitude response of low pass FIR filter with Hamming window');

subplot(2,2,2);
plot(w/pi,abs(Hw));
grid;
xlabel('NormaIised frequency');
ylabel('Phase');
title('Phase response of low pass FIR filter with Hamming window');

%FIR HPF
window =hamming(N+2);
hn = fir1(N,wc/pi, 'high',window);
w=0:0.01:pi;
Hw= freqz(hn,1 ,w);
display('The HPF filter coefficients are');
display(hn);

subplot(2,2,3);
plot(w/pi,20*log10(abs(Hw)));
grid;
xlabel('Normalised frequency');
ylabel('Magnitude');
title(']Magnitude response of high pass FIR filter with Hamming window');
subplot(2,2,4);
plot(w/pi,abs(Hw));
grid;
xlabel('NormaIised frequency');
ylabel('phase');
title('Phase response of high pass FIR filter with Hamming window');

AFTER THE EXECUTION:

Enter the cut off frequency in Hz=200


Enter the sampling frequency in Hz=1200
Enter the order of the filter N=25
The LPF filter coefficients are
hn =

0.0010 -0.0013 -0.0042 -0.0034

0.0055 0.0169 0.0125 -0.0181

-0.0521 -0.0379 0.0581 0.2055

0.3174 0.3174 0.2055 0.0581

-0.0379 -0.0521 -0.0181 0.0125

0.0169 0.0055 -0.0034 -0.0042

-0.0013 0.0010

The HPF filter coefficients are

hn =

-0.0017 -0.0000 0.0033 0.0054

0.0000 -0.0130 -0.0190 -0.0000

0.0387 0.0551 0.0000 -0.1303

-0.2714 0.6653 -0.2714 -0.1303

0.0000 0.0551 0.0387 -0.0000

-0.0190 -0.0130 0.0000 0.0054

0.0033 -0.0000 -0.0017


GRAPH:
HAMMING WINDOW:
PROGRAM
HANNING WINDOW:

clc
clear all
close all
fc = input('Enter the cut off frequency in Hz:');
Fs = input('Enter the sampling frequency in Hz:');
N= input('Enter the order of the filter N=');
wc= ((2*pi*fc)/Fs);
window = hanning(N+1); % window = hanning(N+1) for Hanning window
hn = fir1(N,wc/pi, window);
w=0:0.01:pi;
Hw= freqz(hn,1 ,w);
display('The LPF filter coefficients are');
display(hn);

subplot(2,2,1);
plot(w/pi,20*log(abs(Hw)));
grid;
xlabel('NormaIised frequency');
ylabel('Magnitude');
title( 'Magnitude response of low pass FIR filter ');

subplot(2,2,2);
plot(w/pi,abs(Hw));
grid;
xlabel('NormaIised frequency');
ylabel('Phase');
title('Phase response of low pass FIR filter ');
%FIR HPF
window =hanning(N+2); %window = hanning(N+2) for Hanning window
hn = fir1(N,wc/pi, 'high',window);
w=0:0.01:pi;
Hw= freqz(hn,1 ,w);
display('The HPF filter coefficients are');
display(hn);

subplot(2,2,3);
plot(w/pi,20*log10(abs(Hw)));
grid;
xlabel('Normalised frequency');
ylabel('Magnitude');
title('Magnitude response of high pass FIR filter ');
subplot(2,2,4);
plot(w/pi,abs(Hw));
grid;
xlabel('NormaIised frequency');
ylabel('phase');
title('Phase response of high pass FIR filter ');

AFTER THE EXECUTION:


Enter the cut off frequency in Hz:
200
Enter the sampling frequency in Hz:
1200
Enter the order of the filter N=
25
The LPF filter coefficients are
hn =

0 -0.0002 -0.0019 -0.0023 0.0043 0.0146 0.0115

-0.0172 -0.0504 -0.0372 0.0575 0.2045 0.3166 0.3166

0.2045 0.0575 -0.0372 -0.0504 -0.0172 0.0115 0.0146

0.0043 -0.0023 -0.0019 -0.0002 0

The HPF filter coefficients are


hn =

0 -0.0000 0.0014 0.0035 0.0000 -0.0111 -0.0173

-0.0000 0.0373 0.0540 0.0000 -0.1299 -0.2717 0.6667

-0.2717 -0.1299 0.0000 0.0540 0.0373 -0.0000 -0.0173

-0.0111 0.0000 0.0035 0.0014 -0.0000 0


GRAPH:
HANNING WINDOW
PROGRAM:
BLACKMAN WINDOW:
clc
clear all
close all
fc = input('Enter the cut off frequency in Hz:');
Fs = input('Enter the sampling frequency in Hz:');
N= input('Enter the order of the filter N=');
wc= ((2*pi*fc)/Fs);
window = blackman(N+1); % window = blackman(N+1) for blackman window
hn = fir1(N,wc/pi, window);
w=0:0.01:pi;
Hw= freqz(hn,1 ,w);
display('The LPF filter coefficients are');
display(hn);

subplot(2,2,1);
plot(w/pi,20*log(abs(Hw)));
grid;
xlabel('NormaIised frequency');
ylabel('Magnitude');
title( 'Magnitude response of low pass FIR filter ');

subplot(2,2,2);
plot(w/pi,abs(Hw));
grid;
xlabel('NormaIised frequency');
ylabel('Phase');
title('Phase response of low pass FIR filter ');
%FIR HPF
window =blackman(N+2); % window = blackman(N+1) for blackman window
hn = fir1(N,wc/pi, 'high',window);
w=0:0.01:pi;
Hw= freqz(hn,1 ,w);
display('The HPF filter coefficients are');
display(hn);

subplot(2,2,3);
plot(w/pi,20*log10(abs(Hw)));
grid;
xlabel('Normalised frequency');
ylabel('Magnitude');
title('Magnitude response of high pass FIR filter');
subplot(2,2,4);
plot(w/pi,abs(Hw));
grid;
xlabel('NormaIised frequency');
ylabel('phase');
title('Phase response of high pass FIR filter ');

AFTER THE EXECUTION:


Enter the cut off frequency in Hz=200
Enter the sampling frequency in Hz=1200
Enter the order of the filter N=25
The LPF filter coefficients are

hn =

0 -0.0001 -0.0007 -0.0010 0.0022 0.0085

0.0076 -0.0127 -0.0411 -0.0329 0.0540 0.2001

0.3162 0.3162 0.2001 0.0540 -0.0329 -0.041

-0.0127 0.0076 0.0085 0.0022 -0.0010 -0.0007

-0.0001 0

The HPF filter coefficients are

hn =

0 -0.0000 0.0006 0.0015 0.0000 -0.0063

-0.0111 -0.0000 0.0296 0.0466 0.0000 -0.1252

-0.2691 0.6667 -0.2691 -0.1252 0.0000 0.0466

0.0296 -0.0000 -0.0111 -0.0063 0.0000 0.0015

0.0006 -0.0000 0
GRAPH
BLACKMAN WINDOW:
PROGRAM:
RECTANGULAR WINDOW:
clc
clear all
close all
fc = input('Enter the cut off frequency in Hz:');
Fs = input('Enter the sampling frequency in Hz:');
N= input('Enter the order of the filter N=');
wc= ((2*pi*fc)/Fs);
window = boxcar(N+1); % window = boxcar(N+1) for rectangular window
hn = fir1(N,wc/pi, window);
w=0:0.01:pi;
Hw= freqz(hn,1 ,w);
display('The LPF filter coefficients are');
display(hn);

subplot(2,2,1);
plot(w/pi,20*log(abs(Hw)));
grid;
xlabel('NormaIised frequency');
ylabel('Magnitude');
title( 'Magnitude response of low pass FIR filter ');

subplot(2,2,2);
plot(w/pi,abs(Hw));
grid;
xlabel('NormaIised frequency');
ylabel('Phase');
title('Phase response of low pass FIR filter ');
%FIR HPF
window =boxcar(N+1); % window = boxcar(N+1) for rectangular window
hn = fir1(N,wc/pi, 'high',window);
w=0:0.01:pi;
Hw= freqz(hn,1 ,w);
display('The HPF filter coefficients are');
display(hn);

subplot(2,2,3);
plot(w/pi,20*log10(abs(Hw)));
grid;
xlabel('Normalised frequency');
ylabel('Magnitude');
title('Magnitude response of high pass FIR filter');
subplot(2,2,4);
plot(w/pi,abs(Hw));
grid;
xlabel('NormaIised frequency');
ylabel('phase');
title('Phase response of high pass FIR filter');

AFTER THE EXECUTION:


Enter the cut off frequency in Hz=200
Enter the sampling frequency in Hz=1200
Enter the order of the filter N=25
The LPF filter coefficients are

hn =

0.0131 -0.0142 -0.0312 -0.0172 0.0192 0.0436 0.0252 -0.0297

-0.0727 -0.0467 0.0654 0.2181 0.3271 0.3271 0.2181 0.0654

-0.0467 -0.0727 -0.0297 0.0252 0.0436 0.0192 -0.0172 -0.0312

-0.0142 0.0131

The HPF filter coefficients are

hn =

-0.0206 -0.0000 0.0244 0.0268 0.0000 -0.0335 -0.0383 -0.0000

0.0537 0.0671 0.0000 -0.1342 -0.2684 0.6490 -0.2684 -0.1342

0.0000 0.0671 0.0537 -0.0000 -0.0383 -0.0335 0.0000 0.0268

0.0244 -0.0000 -0.0206


GRAPH:
RECTANGULAR WINDOW:
PROGRAM:
clc
clear all
close all
Ap= input('Enter the pass band attenuation in dB =');
Wp= input('Enter the pass band edge frequency in rad/sec =');
As= input('Enter the stop band attenuation in dB =');
Ws= input('Enter the stop band edge frequency in rad/sec =');
Fs= input('Enter the sampling frequency =');
Ts= 1/Fs;
% To calculate order & cut-off frequency
[n, Wc] = buttord(Wp, Ws, Ap, As,'s');
disp('The order of the Butterworth filter is N= ')
disp(n);
disp('The cut- off frequency of the Butterworth filter is Wc = ');
disp(Wc);
% To obtain analog transfer function of butterworth filter
[num, den] = butter(n, Wc, 's');
disp('The transfer function of the analog Butterworth filter is H(s)= ');
HLs = tf(num, den)
%To obtain system function in digital domain using bilinear transformation
[b,a] = bilinear(num, den, Fs)
disp('The transfer function of the digital Butterworth filten using bilinear transformation is');
HLz = tf(b,a,Ts)
%To obtain system function in digital domain using impulse invariant %transformation
[b,a] = impinvar(num,den,Fs)
disp('The transfer function of the digital Butterworth filter using impulse invaince method is
');
%To obtain frequency response
[Ha, Wa] = freqs(num, den, 1024 );
[Hd, wd] = freqz(b, a, 1024,10 );
figure(1)
plot(Wa, 20*log10(abs(Ha)))
title('Magnitude Frequency Response of analog Butter worth Filter')
xlabel('W…->')
ylabel(' 20*log10|H(W)|')
figure(2)
plot(wd, 20*log10(abs(Hd)))
xlabel('w-->')
ylabel(' 20*log10|H(w)|')
title(' Magnitude Frequency Response of digital Butter worth Filter')
AFTER THE EXECUTION
Enter the pass band attenuation in dB =3.01
Enter the pass band edge frequency in rad/sec =2
Enter the stop band attenuation in dB =15
Enter the stop band edge frequency in rad/sec =4.82
Enter the sampling frequency 1
The order of the Butterworth filter is N= 2
The cut- off frequency of the Butterworth filter is Wc = 2.0490
The transfer function of the analog Butterworth filter is H(s)=

HLs =

4.198
---------------------
s^2 + 2.898 s + 4.198

Continuous-time transfer function.


Model Properties
b=

0.3000 0.6000 0.3000

a=

1.0000 0.0283 0.1717

The transfer function of the digital Butterworth filten using bilinear transformation is

HLz =

0.3 z^2 + 0.6 z + 0.3


------------------------
z^2 + 0.02834 z + 0.1717

Sample time: 1 seconds


Discrete-time transfer function.
Model Properties

b=
0 0.6754 0

a=

1.0000 -0.0571 0.0552

The transfer function of the digital Butterworth filter using impulse invaince method is
EXPECTED GRAPH
PROGRAM
clc
clear all
close all
Ap= input('Enter the passband attenuation in dB =');
Wp= input('Enter the passband edge frequency in rad/sec =');
As= input('Enter the stopband attenuation in dB =');
Ws= input('Enter the stopband edge frequency in rad/sec =');
Fs= input('Enter the sampling frequency =');
Ts= 1/Fs;
% To calculate order & cut-off frequency
[n, Wc] = buttord(Wp, Ws, Ap, As, 's');
disp( 'The order of the Butterworth filter is N= ')
disp(n);
disp('The cut- off frequency of the Butterworth filter is Wc = ');
disp(Wc);
% To obtain analog transfer function of butterworth filter
[num, den] = butter(n, Wc, 'high', 's');
disp('The transfer function of the analog Butterworth filter is H(s)= ');
Hhs = tf(num,den)
%To obtain system function in digital domain using bilinear transformation
[b,a] = bilinear(num,den,Fs)
disp( 'The transfer function of the digital Butterworth filter using bilinear transformation is');
Hhz = tf(b,a, Ts)
%To obtain system function in digital domain using impulse invariant %transformation
%[b,a] = impinvar(num,den,Fs)
%disp(*The transfer function of the digital Butterworth filter using impulse invaince method
is ');
%HLz = tf(b,a,Ts)
% To obtain frequency response
[Ha, Wa] = freqs(num, den, 1024);
[Hd, wd] = freqz(b, a, 1024,10 );
figure(1)
plot(Wa, 20*log10(abs(Ha)))
title('Magnitude Frequency Response of analog Butter worth Filter')
xlabel('W...>')
ylabel('20*log10|H(w)|')
title('Magnitude Frequency Response of digital Butter worth Filter')
figure (2)
plot (wd,20*log10(abs(Hd)))
xlabel('w--- >')
ylabel('20*log10|H(w)|')
title ('magnitude frequency response of digital butter worth filter ')
AFTER THE EXECUTION:
Enter the passband attenuation in dB =3.01
Enter the passband edge frequency in rad/sec =2
Enter the stopband attenuation in dB =15
Enter the stopband edge frequency in rad/sec =4.828
Enter the sampling frequency =1
The order of the Butterworth filter is N= 2

The cut- off frequency of the Butterworth filter is Wc = 2.0524

The transfer function of the analog Butterworth filter is H(s)=

Hhs =

s^2
---------------------
s^2 + 2.902 s + 4.212

Continuous-time transfer function.


Model Properties

b=

0.2854 -0.5707 0.2854

a=

1.0000 0.0303 0.1717

The transfer function of the digital Butterworth filter using bilinear transformation is

Hhz =

0.2854 z^2 - 0.5707 z + 0.2854


------------------------------
z^2 + 0.03028 z + 0.1717

Sample time: 1 seconds


Discrete-time transfer function.
Model Properties
EXPECTED GRAPH
CIRCULAR CONVOLUTION
Circular convolution is another mathematical operation used in signal processing, especially
for circular or periodic signals. The formula for circular convolution of two discrete sequences,
x[n] and h[n], is defined as follows:

y[n] = ∑(x[k] * h[(n - k) mod N])

In this formula:
- y[n] is the output sequence (result of the circular convolution).
- x[n] is the first input sequence.
- h[n] is the second input sequence.
- k is the summation variable, and the summation is taken over all possible values of k.
- N is the length of the sequences x[n] and h[n].

The key difference between linear convolution and circular convolution is the use of modulo
N in the formula. Circular convolution assumes that the sequences x[n] and h[n] are periodic
with a period of N, and it wraps around when reaching the end of the sequence.

Circular convolution is often used in applications involving periodic signals, such as in digital
signal processing for circular buffers or in the context of cyclic or periodic phenomena.

To perform circular convolution efficiently, the Discrete Fourier Transform (DFT) and the
Circular Convolution Theorem can be used. The Circular Convolution Theorem states that
circular convolution in the time domain is equivalent to element-wise multiplication in the
frequency domain after taking the DFT of the sequences, followed by an inverse DFT to obtain
the result in the time domain. This approach is computationally more efficient for long
sequences.
LINEAR CONVOLUTION
Linear convolution is a mathematical operation used in signal processing and mathematics to
combine two sequences to produce a third sequence. The formula for linear convolution
involves summing the products of corresponding elements of the two input sequences as they
slide past each other. Here's the formula:

Given two sequences x[n] and h[n], the linear convolution y[n] is calculated as:

y[n] = ∑(x[k] * h[n - k])

Where:
y[n] is the resulting sequence (output).
x[n] and h[n] are the input sequences.
k is the summation index, typically ranging from negative infinity to positive infinity but
practically limited to the available data.
denotes the multiplication.
In practice, since the sequences are often finite, you'll need to consider only a finite range of
values for k.

Linear convolution is used in various applications like digital signal processing, image
processing, and more, to process and filter signals or data

You might also like