0% found this document useful (0 votes)
1 views5 pages

DSP Codes (7-12)

The document contains MATLAB code for designing various digital filters including Butterworth, Chebyshev, and FIR filters, along with their magnitude and phase responses. It also includes sections on interpolation and decimation for sequences, sampling by a factor, and computing impulse and step responses. Each filter type is implemented with specific parameters and visualized using plots.

Uploaded by

suprajatalamala
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)
1 views5 pages

DSP Codes (7-12)

The document contains MATLAB code for designing various digital filters including Butterworth, Chebyshev, and FIR filters, along with their magnitude and phase responses. It also includes sections on interpolation and decimation for sequences, sampling by a factor, and computing impulse and step responses. Each filter type is implemented with specific parameters and visualized using plots.

Uploaded by

suprajatalamala
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/ 5

DSP CODES (7-12)

BUTTERWORTH FILTER
% LPF
clc;
clear all;
close all;
alphas=30;
alphap=0.5;
fpass=1000;
fstop=1500;
fsam=5000;
wp=2*fpass/fsam;
ws=2*fstop/fsam;
[n,wn]=buttord(wp,ws,alphap,alphas);
[b,a]=butter(n,wn);
[h,w]=freqz(b,a);
subplot(2,1,1);
plot(w/pi,20*log10(abs(h)));
xlabel('Normalized frequency')
ylabel('gain in db')
title('magnitude response')
subplot(2,1,2);
plot(w/pi,angle(h));
xlabel('Normalized frequency')
ylabel('phase in radians')
title('phase response')

%HPF
clc;
clear all;
close all;
alphas=50;
alphap=1;
fpass=1050;
fstop=600;
fsam=3500;
wp=2*fpass/fsam;
ws=2*fstop/fsam;
[n,wn]=buttord(wp,ws,alphap,alphas);
[b,a]=butter(n,wn,'high');
[h,w]=freqz(b,a);subplot(2,1,1);
plot(w/pi,m);
ylabel('gain in db');
xlabel('(a) normalized freq');
subplot(2,1,1);
plot(w/pi,20*log10(abs(h)));
xlabel('Normalized frequency')
ylabel('gain in db')
title('magnitude response')
subplot(2,1,2);
plot(w/pi,angle(h));
xlabel('Normalized frequency')
ylabel('phase in radians')
title('phase response')

CHEBYSHEV FILTER
%LPF
clc;
clear all;
close all;
alphas=0.9;
alphap=0.15;
wp=0.3*pi;
ws=0.5*pi;
[n,wn]=cheb1ord(wp/pi,ws/pi,alphap,alphas);
[b,a]=cheby1(n,alphap,wn);
[h,w]=freqz(b,a);
subplot(2,1,1);
plot(w/pi,20*log10(abs(h)));
xlabel('Normalized frequency')
ylabel('gain in db')
title('magnitude response')
subplot(2,1,2);
plot(w/pi,angle(h));
xlabel('Normalized frequency')
ylabel('phase in radians')
title('phase response')

%HPF

clc;
clear all;
close all;
alphas=0.9;
alphap=0.15;
wp=0.3*pi;
ws=0.5*pi;
[n,wn]=cheb1ord(wp/pi,ws/pi,alphap,alphas);
[b,a]=cheby1(n,alphap,wn);
[h,w]=freqz(b,a);
subplot(2,1,1);
plot(w/pi,20*log10(abs(h)));
xlabel('Normalized frequency')
ylabel('gain in db')
title('magnitude response')
subplot(2,1,2);
plot(w/pi,angle(h));
xlabel('Normalized frequency')
ylabel('phase in radians')
title('phase response')

FIR FILTERS

n=input('enter order of the filter');


fp=input('enter the passband frequency');
fs=input('enter the stopband frequency');
f=input('enter the sampling frequency');
wp=2*fp/f;
ws=2*fs/f;

%RECTANGULAR WINDOW LOW PASS FILTER:-

window=rectwin(n+1);
b=fir1(n,wp,window);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(3,3,1);
plot(o/pi,m);
title('Magnitude response of Rectangular Window LPF ');
xlabel('normalised frequency');
ylabel('gain in db');
grid on;

%RECTANGULAR WINDOW HIGH PASS FILTER:

window=rectwin(n+1);
b=fir1(n,wp,'high',window);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(3,3,2);
plot(o/pi,m);
title('Magnitude response of Rectangular Window HPF');
xlabel('normalised frequency');
ylabel('gain in db');
grid on;

% HAMMING WINDOW LOW PASS FILTER:-


window = hamming(n + 1);
b = fir1(n, wp, window);
[h, o] = freqz(b, 1, 256);
m = 20 * log10(abs(h));
subplot(3, 3, 3);
plot(o/pi, m);
title('Magnitude response of Hamming Window LPF');
xlabel('Normalized frequency');
ylabel('Gain in dB');
grid on;

%KAISER WINDOW LOW PASS FILTER:-

window=kaiser(n+1);
b=fir1(n,wp,window);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(3,3,5);
plot(o/pi,m);
title('Magnitude response of Kaiser Window LPF ');
xlabel('normalised frequency');
ylabel('gain in db');
grid on;

%KAISER WINDOW HIGH PASS FILTER:-


window=kaiser(n+1);
b=fir1(n,wp,'high',window);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(3,3,6);
plot(o/pi,m);
title('Magnitude response of Kaiser Window HPF');
xlabel('normalised frequency');
ylabel('gain in db');
grid on;

INTERPOLATION & DECIMATION (FOR A SEQUENCE)


original_sequence = [1,2,3,4,5,6,7,8];
upsampling_factor = 3;
downsampling_factor = 2;
upsampled_sequence = zeros(1, length(original_sequence) * upsampling_factor);
upsampled_sequence(1:upsampling_factor:end) = original_sequence;
downsampled_sequence =original_sequence(1:downsampling_factor:end);
subplot(3, 1, 1);
stem(original_sequence);
title('Original Sequence');
subplot(3, 1, 2);
stem(downsampled_sequence);
title('Downsampled Sequence');
subplot(3,1,3);
stem(upsampled_sequence);
title('Upsampled Sequence');
disp(original_sequence);
disp('Original Sequence');
disp(downsampled_sequence);
disp('Downsampled Sequence');
disp(upsampled_sequence);
disp('Upsampled Sequence');
SAMPLING BY A FACTOR (for a sequence)
original_signal = [1,2,3,4,5,6,7,8,9];
up_factor=input('Enter the up sampling factor: ');
down_factor=input('Enter the down sampling factor: ');
subplot(2,1,1);
stem(original_signal);
title('Original Signal');
% Upsampling
upsampled_signal = zeros(1, length(original_signal) * up_factor);
upsampled_signal(1:up_factor:end) = original_signal;

% Downsampling
downsampled_signal = upsampled_signal(1:down_factor:end);
subplot(2,1,2);
stem(downsampled_signal);
title('Sample converted signal');
disp(upsampled_signal);
disp(downsampled_signal);

IMPULSE & STEP RESPONSE

% Define the transfer function coefficients


b = [1, -1, 0.9]; % Numerator coefficients in the z-domain
a = 1; % Denominator coefficients (since it's just 1)

N = 4; % Number of points for the step response


impulse_response = impz(b, a, N);
disp(impulse_response)

% Compute the step response in the z-domain


step_response_z = stepz(b, a, N);
disp(step_response_z)
% Plot the step response
n = 0:N-1; % Time indices
subplot(2,1,1);
stem(n, impulse_response);
title("impulse response");
subplot(2,1,2);
stem(n, step_response_z);
xlabel('Time (n)');
ylabel('Step Response in z-domain');
title('Step Response of the System u(n) - u(n-1) + 0.9u(n-2)');

You might also like