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

DSP Codes for Filter Design and Analysis

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)
9 views5 pages

DSP Codes for Filter Design and Analysis

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

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)');

Common questions

Powered by AI

To calculate the impulse response of a digital filter, the z-domain transfer function is defined by its numerator and denominator polynomial coefficients. The 'impz' function computes the impulse response by applying an inverse Z-transform, which is achieved by evaluating the inverse transfer function at each sample point, mapping discretely in the time domain. This method provides the response of the system to a unit impulse input, characterized by its coefficients in the context of the system's behavior .

The phase response of filters is crucial in maintaining signal integrity, especially in applications involving audio signal processing and communications where phase distortion can lead to signal artifacts. Linear phase response is often desired to prevent phase distortion, ensuring that all frequency components arrive simultaneously at the output. This characteristic is essential in systems where the wave shape and timing are critical. Nonlinear phase shifts can introduce undesired warping or smearing effects, affecting the fidelity of the processed signal .

Aliasing occurs in digital signal processing when a continuous signal is under-sampled, resulting in overlapping frequency components that distort the original signal's representation. Filters, especially anti-aliasing filters, are used before sampling to restrict the bandwidth of a signal, thus avoiding frequency overlap by ensuring that high frequencies are adequately attenuated. Post-aliasing, digital filters can also be applied to mitigate aliasing artifacts, though not as effectively due to the irreversible nature of the aliasing error .

Low-pass Butterworth filters allow frequencies below a certain cutoff to pass while attenuating higher frequencies, resulting in a flat passband and a smooth roll-off in the stopband. The magnitude response shows a steep decline past the cutoff frequency, and the phase response starts from zero, respectively. In contrast, a high-pass Butterworth filter allows frequencies above the cutoff to pass, showing a flat response at higher frequencies with attenuation in the lower range. The phase response for high-pass filters starts at a higher frequency and adjusts accordingly .

The transfer function characterizes a digital filter's input-output relationship in the z-domain, providing insights into its behavior across both time and frequency domains. It defines the filter's impulse response and frequency response, encapsulating how the filter will modify spectral content. By analyzing poles and zeros, one can infer stability and frequency selectivity, while the inverse Z-transform relates to the time-domain response, elucidating transient behaviors and ringing effects, thus bridging the temporal and spectral functionalities .

The Kaiser window parameter (β) controls the trade-off between the main lobe width and the side lobe level in the frequency response of an FIR filter. A higher β increases the side lobe attenuation, reducing spectral leakage but widening the main lobe, leading to a smoother transition band. Lower β values result in a narrower main lobe but increased side lobe levels. This parameter provides flexibility, allowing designers to fine-tune the filter to meet specific application requirements .

Rectangular windows have the advantage of simplicity and the narrowest main lobe in the frequency domain, providing sharp cutoffs. However, they suffer from significant side lobes, leading to high spectral leakage. Hamming windows reduce side lobes at the cost of widening the main lobe, providing a better trade-off between main lobe width and side lobe levels. Kaiser windows allow control over the trade-off via a parameter that adjusts the side lobe level and main lobe width, offering flexibility in balancing between sharp roll-off and side lobe suppression .

Chebyshev filters are designed with specific passband ripples and stopband attenuation criteria in mind. The passband ripple (alphap) in a Chebyshev Type I filter allows for ripple within the passband but provides a steeper roll-off than Butterworth filters for a given filter order. The stopband attenuation (alphas) influences the amount of attenuation applied in the stopband, directly impacting filter order and the steepness of the cutoff. These specifications determine the filter's ability to separate desired signals from noise and affect the physical realization of the filter components .

Upsampling involves increasing the sample rate of a digital sequence by inserting zeros between original samples, which can introduce aliasing if not properly filtered. Conversely, downsampling reduces the sample rate by retaining only certain samples, potentially discarding useful information. When combined, they allow for sample rate conversion in digital processing, affecting the sequence length and frequency content by altering the availability of data points for representing the signal .

The order of a Butterworth filter significantly affects its frequency response. A higher order increases the steepness of the filter's roll-off at the cutoff frequency, resulting in a more selective frequency response. The order is determined using the 'buttord' function, which takes into account the passband frequency (wp), stopband frequency (ws), passband ripple (alphap), and stopband attenuation (alphas). The function calculates the minimum filter order needed to meet specific design specifications .

You might also like