0% found this document useful (0 votes)
17 views8 pages

DSP Practical

The document provides MATLAB code for designing various types of filters including Butterworth and Chebyshev filters, as well as downsampling and upsampling techniques. It also includes code for generating different signals such as unit step, ramp, sine, cosine, exponential, sinc, impulse, and signum functions. Additionally, it covers the implementation of FFT and IFFT for signal processing.
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)
17 views8 pages

DSP Practical

The document provides MATLAB code for designing various types of filters including Butterworth and Chebyshev filters, as well as downsampling and upsampling techniques. It also includes code for generating different signals such as unit step, ramp, sine, cosine, exponential, sinc, impulse, and signum functions. Additionally, it covers the implementation of FFT and IFFT for signal processing.
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/ 8

BUTTERWORTH LOW / HIGH PASS FILTER :​

Rp =input("enter th
e ripple at passband in db :")%1
Rs =input("enter the ripple at stopband in db :")%40
Wp = input("enter the passband frequency in radians:")%0.2000
Ws = input("enter the stopband frequency in radians:")%0.8000
[N,Wc] = buttord(Wp,Ws,Rp,Rs)
[b,a] = butter(N,Wc,"high"%low);
W = 0:0.1:pi;
h = freqz(b,a,W)
plot(W,abs(h))
title('Butterworth highpass Filter');xlabel('Frequency
(radians)');ylabel('Magnitude');

BUTTERWORTH BAND STOP / PASS FILTER :


Rp = input('Enter the ripple at passband in dB: ')%1
Rs = input('Enter the ripple at stopband in dB: ')%40
Wp = input('Enter the passband frequencies [Wp1, Wp2] in radians: ')%[0.3,0.7]
Ws = input('Enter the stopband frequencies [Ws1, Ws2] in radians: ')%[0.4,0.6]
[N, Wc] = buttord(Wp, Ws, Rp, Rs)
[b, a] = butter(N, Wc, 'stop');%bandpass
W = 0:0.01:pi;
h = freqz(b, a, W);
plot(W, abs(h));
title('Butterworth Bandstop Filter');xlabel('Frequency
(radians)');ylabel('Magnitude');

CHEBYSHEV 1 LPF / HPF :


Rp = input('Enter the ripple at passband in dB: ')%1
Rs = input('Enter the ripple at stopband in dB: ')%40
Wp = input('Enter the passband frequency in radians: ')%0.2000
Ws = input('Enter the stopband frequency in radians: ')%0.8000
[N, Wc] = cheb1ord(Wp, Ws, Rp, Rs)
[b, a] = cheby1(N, Rp, Wc, 'high');%high
W = 0:0.01:pi;
h = freqz(b, a, W);
plot(W, abs(h));
title('Chebyshev1 LPF');xlabel('Frequency (radians)');ylabel('Magnitude');

CHEBYSHEV 1 BPF / BSF :


Rp = input('Enter the ripple at passband in dB: ')%1
Rs = input('Enter the ripple at stopband in dB: ')%40
Wp = input('Enter the passband frequencies [Wp1, Wp2] in radians: ')%[0.3,0.7]
Ws = input('Enter the stopband frequencies [Ws1, Ws2] in radians: ')%[0.4,0.6]
[N, Wc] = cheb1ord(Wp, Ws, Rp, Rs)
[b, a] = cheby1(N, Rp, Wc, 'bandpass');%stop
W = 0:0.01:pi;
h = freqz(b, a, W);
figure;
plot(W, abs(h));
title('Chebyshev 1 Bandpass Filter');xlabel('Frequency(radians)');ylabel('Magnitude');
CHEBYSHEV 2 LPF / HPF :
Rp = input('Enter the ripple at passband in dB: ')%1
Rs = input('Enter the ripple at stopband in dB: ')%40
Wp = input('Enter the passband frequency in radians: ')%0.2000
Ws = input('Enter the stopband frequency in radians: ')%0.8000
[N, Wc] = cheb2ord(Wp, Ws, Rp, Rs)
[b, a] = cheby2(N, Rs, Wc, 'low');%high
W = 0:0.01:pi;
h = freqz(b, a, W);
plot(W, abs(h));
title('Chebyshev1 LPF');xlabel('Frequency (radians)');ylabel('Magnitude');

CHEBYSHEV 2 BPF / BSF :

Rp = input('Enter the ripple at passband in dB: ')%1


Rs = input('Enter the ripple at stopband in dB: ')%40
Wp = input('Enter the passband frequencies [Wp1, Wp2] in radians: ')%[0.3,0.7]
Ws = input('Enter the stopband frequencies [Ws1, Ws2] in radians: ')%[0.4,0.6]
[N, Wc] = cheb2ord(Wp, Ws, Rp, Rs)
[b, a] = cheby2(N, Rs, Wc, 'bandpass');%stop
W = 0:0.01:pi;
h = freqz(b, a, W);
figure;
plot(W, abs(h));
title('Chebyshev 1 Bandpass Filter');xlabel('Frequency
(radians)');ylabel('Magnitude');

Down Sampling :
#include <stdio.h>
int main() {
int N = 10, M = 3, x[10] = {0,1,2,3,4,5,6,7,8,9};
int y[10] = {0}, i, len = N / M;
for(i = 1; i <= len; i++) {
y[i] = x[M * i];
}
for(i = 0; i <= len; i++) {
printf("%d\t", y[i]);
}
return 0;
}

Up Sampling
#include <stdio.h>
int main() {
int Sr = 4, N = 10;
int x[10] = {0,1,2,3,4,5,6,7,8,9};
int y[50] = {0}, len = N * Sr;
int i, k = Sr - 1, pad = Sr - 1;
y[0] = x[0];
for(i = 1; i < len; i++) {
if(i % Sr == 0) {
y[i] = x[i - k];
k += pad;
} else {
y[i] = 0;
}
}
for(i = 0; i < len; i++) {
printf("%d ", y[i]);
}
return 0;
}

BUTTERWORTH LOW PASS FILTER USING IMPLUSE INVARIANT


Wp = 0.2 * pi; % Digital Passband edge (rad/sample)
Ws = 0.6 * pi; % Digital Stopband edge (rad/sample)
Rp = -20 * log10(0.8); % Passband ripple (dB)
Rs = -20 * log10(0.2); % Stopband attenuation (dB)
Wp_norm = Wp / pi;
Ws_norm = Ws / pi;
[N, Wn] = buttord(Wp_norm, Ws_norm, Rp, Rs);
disp(N);
cutoff_freq = Wp/ ((10^(Rp/10) - 1)^(1/(2*N)))
[bs, as] = butter(N, cutoff_freq, 's')
[bz, az] = impinvar(bs, as, 1)
[H] = freqz(bz, az, 1024, 'half', 1);
plot(abs(H), 'b', 'LineWidth', 1.5);
title('Butterworth Low-Pass Filter (Impulse Invariant Method)');
xlabel('Frequency (Hz)');
ylabel('Magnitude');

BUTTERWORTH LOW PASS FILTER USING BILINEAR TRANSFORMATION:

Wp = 0.5 * pi; % Digital PRssband edge (rad/sample)


Ws = 0.75 * pi; % Digital Stopband edge (rad/sample)
Rp = -20 * log10(0.707); % Passband ripple (dB)
Rs = -20 * log10(0.2); % Stopband attenuation (dB)
Warped = (2) * tan(Wp/2);
Wp_norm = Wp / pi;
Ws_norm = Ws / pi;
[N, Wn] = buttord(Wp_norm, Ws_norm, Rp, Rs);
disp(N);
cutoff_freq = Warped / ((10^(Rp/10) - 1)^(1/(2*N)));
[bs,as] = butter(N, cutoff_freq, 's')
[bz, az] = bilinear(bs, as, 1)
H = freqz(bz, az, 1024, 'half', 1);
plot(abs(H), 'b', 'LineWidth', 1.5);
title('Butterworth Low-PRss Filter (Bilinear Transformation)');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
4.BAND PASS REJECT FILTER USING FOURIER SERIES:
% Input parameters
N = input('Enter the Length of the filter: ')
Wc1 = input('Enter the lower cutoff frequency (in radians): ')
Wc2 = input('Enter the upper cutoff frequency (in radians): ')
n = 0:N-1;
a = (N-1)/2;
c = 0.0001;
h = (sin(Wc1*(n-a+c)) + sin(pi*(n-a+c))-sin(Wc2*(n-a+c))) ./ (pi*(n-a+c))
w = 0:0.01:pi;
H = freqz(h, 1, w);
plot(w, abs(H))
title('Band-Reject FIR Filter Frequency Response');
xlabel('Frequency (rad/sample)');
ylabel('Magnitude');

5.FFT:
x = input("Input sequence:");
N = input("Number of points for DFT:");
L = length(x);
if N >= L
x = [x, zeros(1, N - L)];
z = fft(x)
stem(0:N-1, abs(z));
title('Magnitude Spectrum');
xlabel('Frequency Index');
ylabel('|X(k)|');
grid on;
else
error('Give proper input: N should be greater than or equal to the length of
the input sequence');
end

6.IFFT:
x = input("Input sequence:");
N = input("Number of points for IDFT:");
L = length(x);
if N >= L
x = [x, zeros(1, N - L)];
z =ifft(x)
abs(z)
figure;
stem(0:N-1, abs(z));
title('Magnitude Spectrum');
xlabel('TIME');
ylabel('|X(n)|');
grid on;
else
error('Give proper input: N should be greater than or equal to the length of
the input sequence');
end

UNIT STEP
t = linspace(-10, 10, 100); % time vector
unit_step = t >= 0; % function
subplot(2,1,1);
plot(t,unit_step);
title('Unit Step Signal');
subplot(2,1,2);
stem(t, unit_step);
title('Unit Step Signal');
xlabel('Time');
ylabel('Amplitude');

RAMP
time = 0:10
ramp_function = time .* (time >= 0);
subplot(2,1,1);
plot(time, ramp_function);
title('Ramp Signal');
subplot(2,1,2);
stem(time, ramp_function);
title('Ramp Signal');
xlabel('Time');
ylabel('Amplitude');
SIN
sine = sin(t);
subplot(2,1,1);
plot(t, sine);
title('Sine Signal');
subplot(2,1,2);
stem(t, sine);
title('Sine Signal');
xlabel('Time');
ylabel('Amplitude');

COS
cosine = cos(t);
subplot(2,1,1);
plot(t, cosine);
title('Cosine Signal');
subplot(2,1,2);
stem(t, cosine);
title('Cosine Signal');
xlabel('Time');
ylabel('Amplitude');

IEXPO
i_exp = exp(t);
subplot(2,1,1);
plot(t, i_exp);
title('Increasing Exponential Signal');
subplot(2,1,2);
stem(t, i_exp);
title('Increasing Exponential Signal');
xlabel('Time');
ylabel('Amplitude');

DEXPO
d_exp = exp(-t);
subplot(2,1,1);
plot(t, d_exp);
title('Decreasing Exponential Signal');
subplot(2,1,2);
stem(t, d_exp);
title('Decreasing Exponential Signal');
xlabel('Time');
ylabel('Amplitude');
SINC:
t = linspace(-10, 10, 100);
y = sinc(t);
figure;
x= sinc(t);
subplot(2,1,1);
plot(t, y, 'b', 'LineWidth', 1);
title('Sinc Signal');
subplot(2,1,2);
stem(t,x);
title('Sinc Signal');
xlabel('Time');
ylabel('Amplitude');

IMPLUSE
ts = -1000:1000;
impulse_function = (ts == 0);
subplot(2,1,1);
plot(ts, impulse_function);
title('Impulse Signal');
subplot(2,1,2);
stem(ts, impulse_function);
title('Impulse Signal');
xlabel('Time');
ylabel('Amplitude');

SIGNUM
signum = sign(t);
subplot(2,1,1);
plot(t, signum);
title('Signum Signal');
subplot(2,1,2);
stem(t, signum);
title('Signum Signal');
xlabel('Time');
ylabel('Amplitude');

You might also like