0% found this document useful (0 votes)
5 views23 pages

DSP Codes

The document contains MATLAB code for generating various discrete-time signals including sawtooth, triangular, sinusoidal, square, and special signals like exponential and Gaussian. It also includes functions for convolution, circular convolution, and discrete Fourier transforms (DFT and IDFT), along with graphical representations of the signals and their transformations. Additionally, there are app functionalities for linear and circular convolution using a graphical user interface.
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)
5 views23 pages

DSP Codes

The document contains MATLAB code for generating various discrete-time signals including sawtooth, triangular, sinusoidal, square, and special signals like exponential and Gaussian. It also includes functions for convolution, circular convolution, and discrete Fourier transforms (DFT and IDFT), along with graphical representations of the signals and their transformations. Additionally, there are app functionalities for linear and circular convolution using a graphical user interface.
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/ 23

1.

Generation_of_DT_Sawtooth_and_Triangular_Signals
clc;clear;close all;
n = 0:pi/20:2*pi*3;
subplot(211);
x = sawtooth(n);
stem(n,x,'r',linewidth=1.5);
xlabel('Time');
ylabel('Amplitude');
title('Discrete time Sawtooth Signal');

subplot(212);
x = sawtooth(n,0.5);
stem(n,x,'b',linewidth=1.5);
xlabel('Time');
ylabel('Amplitude');
title('Discrete time Triangular Signal');

2. Generation_of_DTsignals

clc;clear;close all;

n=-10:1:10;
x1=1;x2=0;
x=x1.*(n>=0)+x2.*(n~=0);
subplot(2,2,1);
stem(n,x,'r',linewidth=1.5);
xlabel('Time');
ylabel('Amplitude');
title('Discrete time step signal');
grid on;

n=-10:1:10;
x1=1;x2=0;
x=x1.*(n==0)+x2.*(n~=0);
subplot(2,2,2);
stem(n,x,'b',linewidth=1.5);
xlabel('Time');
ylabel('Amplitude');
title('Discrete time impulse signal');
grid on;

n=-10:1:10;
x1=1;x2=0;
x=n.*(n>=0)+x2.*(n~=0);
subplot(2,2,3);
stem(n,x,'g',linewidth=1.5);
xlabel('Time');
ylabel('Amplitude');
title('Discrete time ramp signal');
grid on;

n=-10:1:10;
x1=1;x2=0;
x=((n.*n)/2);%.*(abs(n)>=0)+x2.*(abs(n)<0);
subplot(2,2,4);
stem(n,x,'k',linewidth=1.5);
xlabel('Time');
ylabel('Amplitude');
title('Discrete time Parabolic signal');
grid on;

3. Generation_of_Sinusoidal_and_Cosine_signals

clc;clear;close all;
m = input('Enter the number of cycles: ');
n = 0:pi/30:2*pi*m;
x = sin(n);
subplot(211);
stem(n,x,'r',LineWidth=1.5);
xlabel('Time');
ylabel('Amplitude');
title('Discrete time periodic Sine wave');

x = cos(n);
subplot(212);
stem(n,x,'r',LineWidth=1.5);
xlabel('Time');
ylabel('Amplitude');
title('Discrete time periodic Cosine wave');

4. Generation_of_Square

clc;clear;close all;
n = 0:pi/20:2*pi*3;
subplot(211);
x = square(n);
stem(n,x,'r',linewidth=1.5);
xlabel('Time');
ylabel('Amplitude');
title('Discrete time Square Signal');
grid on;

5. special_signals

clc;
clear;
close all;
%Exponentially increasing
n = -10:0.2:10;
x_exp1 = exp(n);
subplot(2,1,1);
stem(n,x_exp1,'r',LineWidth=1.5);
xlabel('Time');
ylabel('Amplitude');
title('Exponentially increasing');
grid on;

%Exponentially Decreasing

x_exp2 = exp(-n);
subplot(2,1,2);
stem(n,x_exp2,'k',LineWidth=1.5);
xlabel('Time');
ylabel('Amplitude');
title('Exponentially Decreasing');
grid on;

figure;
%Absolutle Rectangle
n = -10:10;
x1 = 1;x2 = 0;
x_rec = x1.*(abs(n)<=10) + x2.*(abs(n)>10);
subplot(2,1,1);
stem(n,x_rec,'r',LineWidth=1.5);
xlabel('Time');
ylabel('Amplitude');
title('Absolutle Rectangle');
grid on;

%Absolutle triangle
A = 10;
x_tri = 1-abs(n)/A;
subplot(2,1,2);
stem(n,x_tri,'k',LineWidth=1.5);
xlabel('Time');
ylabel('Amplitude');
title('Absolutle triangle');
grid on;

%sync
figure;
n = -10:0.2:10;
x_sinc = sinc(n);
stem(n,x_sinc,'r',linewidth=1.5);
xlabel('Time');
ylabel('Amplitude');
title('Sinc');
grid on;

%Gaussian
figure;
n = -5:0.2:5;
x_gaussain = exp(-1.*(n.^2));
stem(n,x_gaussain,'r',linewidth=1.5);
xlabel('Time');
ylabel('Amplitude');
title('Gaussain');
grid on;

%signum
figure;
n = -10:1:10;
x1 = 1;x2 = 0;x3 = -1;
x_sgn = x1.*(n>0) + x2.*(n == 0) + x3.*(n<0);
stem(n,x_sgn,'r',linewidth=1.5);
xlabel('Time');
ylabel('Amplitude');
title('Signum');
grid on;
6. Conv

clc;
clear;
close all;

x = input('Enter x[n] : ');


h = input('Enter h[n] : ');

Nx = length(x);
Nh = length(h);

nx = 0:Nx-1;
nh = 0:Nh-1;

N = Nx+Nh-1;
n = 0:N-1;

subplot(3,1,1);
stem(nx,x,'r',LineWidth=1.5);
axis([0 5 -5 5]);
xlabel('Time');
ylabel('Amplitude');
title('x[n]');
grid on;

subplot(3,1,2);
stem(nh,h,'b',LineWidth=1.5);
axis([0 5 -5 5]);
xlabel('Time');
ylabel('Amplitude');
title('h[n]');
grid on;

y = conv(x,h);

disp('The convolved sequence is:');


disp(y);
subplot(3,1,3);
stem(n,y,'b',LineWidth=1.5);
xlabel('Time');
ylabel('Amplitude');
title('X[n]');
grid on;
axis([0 5 -5 10]);

7. Conv_non_zero_index

clc;
clear;
close all;

x = input('Enter x[n] : ');


mx = input('Enter starting index of x[n] : ');
h = input('Enter h[n] : ');
mh = input('Enter starting index of h[n] : ');
Nx = length(x);
Nh = length(h);

nx = mx:Nx+mx-1;
nh = mh:Nh+mh-1;

N = Nx+Nh-1;
m = mx + mh;
n = m:N+m-1;

subplot(3,1,1);
stem(nx,x,'r',LineWidth=1.5);
axis([-2 5 -5 5]);
xlabel('Time');
ylabel('Amplitude');
title('x[n]');
grid on;

subplot(3,1,2);
stem(nh,h,'b',LineWidth=1.5);
axis([-2 5 -5 5]);
xlabel('Time');
ylabel('Amplitude');
title('h[n]');
grid on;

y = conv(x,h);

disp('The convolved sequence is:');


disp(y);
subplot(3,1,3);
stem(n,y,'b',LineWidth=1.5);
xlabel('Time');
ylabel('Amplitude');
title('X[n]');
grid on;
axis([-3 5 -5 10]);

8. Linear_conv

clc;
clear;
close all;

x = input('Enter x[n] : ');


h = input('Enter h[n] : ');

Nx = length(x);
Nh = length(h);

nx = 0:Nx-1;
nh = 0:Nh-1;

N = Nx+Nh-1;
n = 0:N-1;

subplot(3,1,1);
stem(nx,x,'r',LineWidth=1.5);
axis([0 5 -5 5]);
xlabel('Time');
ylabel('Amplitude');
title('x[n]');
grid on;

subplot(3,1,2);
stem(nh,h,'b',LineWidth=1.5);
axis([0 5 -5 5]);
xlabel('Time');
ylabel('Amplitude');
title('h[n]');
grid on;

h1 = h(Nh-nh);

for i=1:N
h2 = [zeros(1,i-1) h1 zeros(1,N-i)];
x1 = [zeros(1,Nh-1) x zeros(1,Nh-1)];
y(i) = x1*h2';

end
disp('The convolved sequence is:');
disp(y);
subplot(3,1,3);
stem(n,y,'b',LineWidth=1.5);
xlabel('Time');
ylabel('Amplitude');
title('X[n]');
grid on;
axis([0 5 -5 10]);

9. CCONV

clc;
clear;
close all;
x = input('Enter x[n] : ');
h = input('Enter h[n] : ');

Nx = length(x);
Nh = length(h);

nx = 0:Nx-1;
nh = 0:Nh-1;

N = max(Nx,Nh);
n = 0:N-1;

y = cconv(x,h,N);

subplot(3,1,1);
stem(nx,x,'r',LineWidth=1.5);
%axis([0 5 -5 5]);
xlabel('Time');
ylabel('Amplitude');
title('x[n]');
grid on;
subplot(3,1,2);
stem(nh,h,'b',LineWidth=1.5);
%axis([0 5 -5 5]);
xlabel('Time');
ylabel('Amplitude');
title('h[n]');
grid on;

disp('The convolved sequence is:');


disp(y);
subplot(3,1,3);
stem(n,y,'b',LineWidth=1.5);
xlabel('Time');
ylabel('Amplitude');
title('X[n]');
grid on;
%axis([0 5 -5 10]);

10. Circular_conv_Matrix_method
clc;
clear;
close all;

x = input('Enter x[n] : ');


h = input('Enter h[n] : ');

Nx = length(x);
Nh = length(h);

nx = 0:Nx-1;
nh = 0:Nh-1;

N = max(Nx,Nh);
n = 0:N-1;

y = circularConv(x,h,N);

subplot(3,1,1);
stem(nx,x,'r',LineWidth=1.5);
%axis([0 5 -5 5]);
xlabel('Time');
ylabel('Amplitude');
title('x[n]');
grid on;

subplot(3,1,2);
stem(nh,h,'b',LineWidth=1.5);
%axis([0 5 -5 5]);
xlabel('Time');
ylabel('Amplitude');
title('h[n]');
grid on;

disp('The convolved sequence is:');


disp(y);
subplot(3,1,3);
stem(n,y,'b',LineWidth=1.5);
xlabel('Time');
ylabel('Amplitude');
title('X[n]');
grid on;
%axis([0 5 -5 10]);

circularConv function

%convolution function
function[y] = circularConv(x,h,N)
nx = length(x);
nh = length(h);
x = [x , zeros(N-nx)];
h = [h, zeros(N-nh)];

for n = 1:N
k = n-1;
p = 0:N-1;
disp(p);
q = mod(p-k,N);
disp(q);
hk = h(q+1);
H(n,:) = hk;
end
y = x*H;

11. app1_linear.mlapp

function LinearButtonPushed(app, event)


xstr = app.xEditField.Value;
hstr = app.hEditField.Value;
x = str2num(xstr);
h = str2num(hstr);
Nx = length(x);
Nh = length(h);
N = Nx + Nh - 1;
n = 0:N-1;
y = conv(x,h);
stem(app.UIAxes,n,y,'r',LineWidth=1.5);
ystr = num2str(y);
app.Label.Text = ystr;
end

% Button pushed function: CircularButton


function CircularButtonPushed(app, event)
xstr = app.xEditField.Value;
hstr = app.hEditField.Value;
x = str2num(xstr);
h = str2num(hstr);
Nx = length(x);
Nh = length(h);
N = max(Nx,Nh);
n = 0:N-1;
y = cconv(x,h,N);
stem(app.UIAxes2,n,y,'b',LineWidth=1.5);
ystr = num2str(y);
app.Label2.Text = ystr;
end

% Button pushed function: ResetButton


function ResetButtonPushed(app, event)
app.xEditField.Value = ' ';
app.hEditField.Value = ' ';
app.Label.Text = ' ';
app.Label2.Text = ' ';
cla(app.UIAxes);
cla(app.UIAxes2);
end

12. D_FT
clc;
clear;
close all;
x = input('Enter x sequence : ');
N = input('Enter No. of samples : ');
n = 0 : N-1;
X = dft(x,N);
mag = abs(X);
phase = angle(X);
disp("DFT is : ");
disp(X);
subplot(2,2,1);
stem(n,x,'r',LineWidth=1.5);
xlabel('No. Of samples');
ylabel('Amplitude');
title('x[n] (1602-22-735-090)');
grid on;
subplot(2,2,2);
stem(n,X,'b',LineWidth=1.5);
xlabel('No. Of samples');
ylabel('Amplitude');
title('X[k] (1602-22-735-090)');
grid on;

subplot(2,2,3);
stem(mag,'k',LineWidth=1.5);
xlabel('No. Of samples');
ylabel('Amplitude');
title('Magnitude (1602-22-735-090)');
grid on;

subplot(2,2,4);
stem(phase,'g',LineWidth=1.5);
xlabel('No. Of samples');
ylabel('Amplitude');
title('Phase (1602-22-735-90)');
grid on;

function [X] = dft(x,N)


L = length(x);
x = [x zeros(1,N-L)];
for k = 0:N-1
for n = 0:N-1
p = exp(-1i*2*pi*n*k/N);
w(k+1,n+1) = p;
end
end
X = w*x';

13. ID_ft
clc;
clear;
close all;
X = input('Enter X sequence : ');
N = input('Enter No. of samples : ');
n = 0:N-1;
x = idft(X,N);
disp("IDFT is : ");
disp(x);
subplot(2,1,1);
stem(n,x,'r',LineWidth=1.5);
xlabel('No. Of samples');
ylabel('Amplitude');
title('x[n] (1602-22-735-090)');
grid on;

subplot(2,1,2);
stem(n,X,'b',LineWidth=1.5);
xlabel('No. Of samples');
ylabel('Amplitude');
title('X[k] (1602-22-735-090)');
grid on;
function [x] = idft(X,N)
for n = 0:N-1
for k = 0:N-1
p = exp(1i*2*pi*n*k/N)/N;
w(k+1,n+1) = p;
end
end
x = w*X';

app2_IDFT.mlapp

function IDFTButtonPushed(app, event)


Xstr = app.XEditField.Value;
X = str2num(Xstr);
x = ifft(X);
xstr = num2str(x);
N = input('Enter No. of samples : ');
n = 0:N-1;
app.Label.Text = xstr;
stem(app.UIAxes,n,X,LineWidth=1.5);
stem(app.UIAxes2,n,x,LineWidth=1.5);
end
app_DFT.mlapp

function DFTButtonPushed(app, event)


xstr = app.xEditField.Value;
x = str2num(xstr);
N = length(x);
k = 0:N-1;
n = 0:N-1;
X = fft(x);
Xstr = num2str(X);
app.Label.Text = Xstr;
mag = abs(X);
phase = angle(X);
stem(app.UIAxes,k,mag,LineWidth=1.5);
stem(app.UIAxes2,phase);
end

13. fir_LPF
clc;
clear;
close all;
N = input('Enter period : ');
fc = input('Enter cutoff Frequency : ');
FS = input('Enter sampling Frequency : ');
wc = 2*pi*fc/FS;
b1 = fir1(N,wc,rectwin(N+1));
[h1,w] = freqz(b1,1,256);
m1 = 20*log10(abs(h1));
plot(w/pi,m1,'b',LineWidth=1.5);
grid on;
xlabel('Frequency');
ylabel('Amplitude');
title('FIR Low Pass Filter (1602-22-735-090)');
hold on;

b2 = fir1(N,wc,triang(N+1));
[h2,w] = freqz(b2,1,256);
m2 = 20*log10(abs(h2));
plot(w/pi,m2,'r',LineWidth=1.5);
grid on;
xlabel('Frequency');
ylabel('Amplitude');
title('FIR Low Pass Filter (1602-22-735-090)');

b3 = fir1(N,wc,hanning(N+1));
[h3,w] = freqz(b3,1,256);
m3 = 20*log10(abs(h3));
plot(w/pi,m3,'k',LineWidth=1.5);
grid on;
xlabel('Frequency');
ylabel('Amplitude');
title('FIR Low Pass Filter (1602-22-735-090)');

hold off;
legend('Rectangular window','Triangular window','Hanning window');

14. fir_HPF
clc;
clear;
close all;
N = input('Enter period : ');
fc = input('Enter cutoff Frequency : ');
FS = input('Enter sampling Frequency : ');
wc = 2*pi*fc/FS;
b1 = fir1(N,wc,'high',rectwin(N+1));
[h1,w] = freqz(b1,1,256);
m1 = 20*log10(abs(h1));
plot(w/pi,m1,'b',LineWidth=1.5);
grid on;
xlabel('Frequency');
ylabel('Amplitude');
title('FIR High Pass Filter (1602-22-735-090)');
hold on;

b2 = fir1(N,wc,'high',triang(N+1));
[h2,w] = freqz(b2,1,256);
m2 = 20*log10(abs(h2));
plot(w/pi,m2,'r',LineWidth=1.5);
grid on;
xlabel('Frequency');
ylabel('Amplitude');
title('FIR High Pass Filter (1602-22-735-116)');
b3 = fir1(N,wc,'high',hanning(N+1));
[h3,w] = freqz(b3,1,256);
m3 = 20*log10(abs(h3));
plot(w/pi,m3,'k',LineWidth=1.5);
grid on;
xlabel('Frequency');
ylabel('Amplitude');
title('FIR High Pass Filter (1602-22-735-090)');

hold off;
legend('Rectangular window','Triangular window','Hanning window');

15. fir_BPF
clc;
clear;
close all;
N = input('Enter period : ');
fc = input('Enter cutoff Frequency : ');
FS = input('Enter sampling Frequency : ');
wc = 2*pi*fc/FS;
b1 = fir1(N,wc,rectwin(N+1));
[h1,w] = freqz(b1,1,256);
m1 = 20*log10(abs(h1));
plot(w/pi,m1,'b',LineWidth=1.5);
grid on;
xlabel('Frequency');
ylabel('Amplitude');
title('FIR Band Pass Filter (1602-22-735-116)');
hold on;

b2 = fir1(N,wc,blackman(N+1));
[h2,w] = freqz(b2,1,256);
m2 = 20*log10(abs(h2));
plot(w/pi,m2,'r',LineWidth=1.5);
grid on;
xlabel('Frequency');
ylabel('Amplitude');
title('FIR Band Pass Filter (1602-22-735-116)');

b3 = fir1(N,wc,hamming(N+1));
[h3,w] = freqz(b3,1,256);
m3 = 20*log10(abs(h3));
plot(w/pi,m3,'k',LineWidth=1.5);
grid on;
xlabel('Frequency');
ylabel('Amplitude');
title('FIR Band Pass Filter (1602-22-735-090)');

hold off;
legend('Rectangular window','Blackmen window','Hamming window');
16. fir_BEP

clc;
clear;
close all;
N = input('Enter period : ');
fc = input('Enter cutoff Frequency : ');
FS = input('Enter sampling Frequency : ');
wc = 2*pi*fc/FS;
b1 = fir1(N,wc,rectwin(N+1));
[h1,w] = freqz(b1,1,256);
m1 = 20*log10(abs(h1));
plot(w/pi,m1,'b',LineWidth=1.5);
grid on;
xlabel('Frequency');
ylabel('Amplitude');
title('FIR Band Pass Filter (1602-22-735-116)');
hold on;

b2 = fir1(N,wc,blackman(N+1));
[h2,w] = freqz(b2,1,256);
m2 = 20*log10(abs(h2));
plot(w/pi,m2,'r',LineWidth=1.5);
grid on;
xlabel('Frequency');
ylabel('Amplitude');
title('FIR Band Pass Filter (1602-22-735-116)');

b3 = fir1(N,wc,hamming(N+1));
[h3,w] = freqz(b3,1,256);
m3 = 20*log10(abs(h3));
plot(w/pi,m3,'k',LineWidth=1.5);
grid on;
xlabel('Frequency');
ylabel('Amplitude');
title('FIR Band Pass Filter (1602-22-735-090)');

hold off;
legend('Rectangular window','Blackmen window','Hamming window');

17. filter_LPF
clc;
clear;
close all;

FS = 1000;
f = 50;
t = 0:1/FS:1;
A = 1;
SNR = 10;

sine_signal = A*sin(2*pi*f*t);
noisy_sig = awgn(sine_signal,SNR,'measured','linear');

N = 50;
fc = 80;
b = fir1(N,fc/(FS/2),'low');

filter_sig = filter(b,1,noisy_sig);

figure;
subplot(3,1,1);
plot(t,sine_signal);
xlabel('Time');
ylabel('Amplitude');
title('Original Sinusoidal signal (1602-22-735-090)');

subplot(3,1,2);
plot(t,noisy_sig);
xlabel('Time');
ylabel('Amplitude');
title('Original Noise signal (1602-22-735-090)');

subplot(3,1,3);
plot(t,filter_sig);
xlabel('Time');
ylabel('Amplitude');
title('Filtered signal (1602-22-735-090)');

18. filter_HPF
clc;
clear;
close all;

FS = 1500;
f = 50;
t = 0:1/FS:1;
A = 1;
SNR = 10;

sine_signal = A*sin(2*pi*f*t);

noisy_sig = awgn(sine_signal,SNR,'measured','linear');

N = 50;
fc = 80;
b = fir1(N,fc/(FS/2),'high');

filter_sig = filter(b,1,noisy_sig);

figure;
subplot(3,1,1);
plot(t,sine_signal);
xlabel('Time');
ylabel('Amplitude');
title('Original Sinusoidal signal (1602-22-735-090)');

subplot(3,1,2);
plot(t,noisy_sig);
xlabel('Time');
ylabel('Amplitude');
title('Original Noise signal (1602-22-735-090)');

subplot(3,1,3);
plot(t,filter_sig);
xlabel('Time');
ylabel('Amplitude');
title('Filtered signal (1602-22-735-090)');

19. filter_BPF
clc;
clear;
close all;

FS = 1000;
f = 50;
t = 0:1/FS:1;
A = 1;
SNR = 10;

sine_signal = A*sin(2*pi*f*t);

noisy_sig = awgn(sine_signal,SNR,'measured','linear');

N = 50;
f_low = 40;
f_high = 60;
fc = [f_low f_high];
b = fir1(N,fc/(FS/2),'bandpass');

filter_sig = filter(b,1,noisy_sig);

figure;
subplot(3,1,1);
plot(t,sine_signal);
xlabel('Time');
ylabel('Amplitude');
title('Original Sinusoidal signal (1602-22-735-090)');

subplot(3,1,2);
plot(t,noisy_sig);
xlabel('Time');
ylabel('Amplitude');
title('Original Noise signal (1602-22-735-090)');

subplot(3,1,3);
plot(t,filter_sig);
xlabel('Time');
ylabel('Amplitude');
title('Filtered signal (1602-22-735-090)');

20. filter_BEF
clc;
clear;
close all;

FS = 1000;
f = 50;
t = 0:1/FS:1;
A = 1;
SNR = 10;

sine_signal = A*sin(2*pi*f*t);

noisy_sig = awgn(sine_signal,SNR,'measured','linear');

N = 50;
f_low = 45;
f_high = 55;
fc = [f_low f_high];
b = fir1(N,fc/(FS/2),'stop');

filter_sig = filter(b,1,noisy_sig);

figure;
subplot(3,1,1);
plot(t,sine_signal);
xlabel('Time');
ylabel('Amplitude');
title('Original Sinusoidal signal (1602-22-735-090)');

subplot(3,1,2);
plot(t,noisy_sig);
xlabel('Time');
ylabel('Amplitude');
title('Original Noise signal (1602-22-735-090)');

subplot(3,1,3);
plot(t,filter_sig);
xlabel('Time');
ylabel('Amplitude');
title('Filtered signal (1602-22-735-090)');

21. IIR_LOW
clc;
clear;
close all;

FS = 1000;
f = 50;
t = 0:1/FS:1;
A = 1;
SNR = 10;

sine_signal = A*sin(2*pi*f*t);

noisy_sig = awgn(sine_signal,SNR,'measured','linear');

N = 50;
f_low = 45;
f_high = 55;
fc = [f_low f_high];
b = fir1(N,fc/(FS/2),'stop');

filter_sig = filter(b,1,noisy_sig);

figure;
subplot(3,1,1);
plot(t,sine_signal);
xlabel('Time');
ylabel('Amplitude');
title('Original Sinusoidal signal (1602-22-735-090)');

subplot(3,1,2);
plot(t,noisy_sig);
xlabel('Time');
ylabel('Amplitude');
title('Original Noise signal (1602-22-735-090)');

subplot(3,1,3);
plot(t,filter_sig);
xlabel('Time');
ylabel('Amplitude');
title('Filtered signal (1602-22-735-090)');

22. IIR_HIGH
clc;
clear;
close all;
rp = input('Enter the pass band ripple : ');
rs = input('Enter the stop band attenuation : ');
wp = input('Enter the pass band edge frequency : ');
ws = input('Enter the stop band edge frequency : ');
fs = input('Enter the sampling frequency : ');
w1 = 2*wp/fs;
w2 = 2*ws/fs;
[n1,wn1] = buttord(w1,w2,rp,rs);
disp('Order of Butterworth Filter');
disp(n1);
disp(wn1);
[b1,a1] = butter(n1,wn1,'High');
[h1,om] = freqz(b1,a1);
mag1 = 20*log(abs(h1));
ang1 = angle(h1);

[n2,wn2] = cheb1ord(w1,w2,rp,rs);
disp('Order of Chebyshew type 1 Filter');
disp(n2);
disp(wn2);
[b2,a2] = cheby1(n2,rp,wn2,'high');
[h2,om] = freqz(b2,a2);
mag2 = 20*log(abs(h2));
ang2 = angle(h2);

[n3,wn3] = cheb1ord(w1,w2,rp,rs);
disp('Order of Chebyshew type 2 Filter');
disp(n3);
disp(wn3);
[b3,a3] = cheby2(n3,rs,wn3,'high');
[h3,om] = freqz(b3,a3);
mag3 = 20*log(abs(h3));
ang3 = angle(h3);

figure;
subplot(3,1,1);
plot(om/pi,mag1,LineWidth=2);
xlabel('n');
ylabel('magnitude');
title('IIR HIGH FILTER(1602-22-735-077)');
subplot(3,1,2);
plot(om/pi,mag2,LineWidth=2);
xlabel('n');
ylabel('magnitude');
title('IIR HIGH FILTER(1602-22-735-077)');
subplot(3,1,3);
plot(om/pi,mag3,LineWidth=2);
xlabel('n');
ylabel('magnitude');
title('IIR HIGH FILTER(1602-22-735-077)');

23. IIR_BANDPASS
clc;
clear;
close all;
rp = input('Enter the pass band ripple : ');
rs = input('Enter the stop band attenuation : ');
wp = input('Enter the pass band edge frequency : ');
ws = input('Enter the stop band edge frequency : ');
fs = input('Enter the sampling frequency : ');
w1 = 2*wp/fs;
w2 = 2*ws/fs;
[n1,wn1] = buttord(w1,w2,rp,rs);
disp('Order of Butterworth Filter');
disp(n1);
disp(wn1);
[b1,a1] = butter(n1,wn1,'bandpass');
[h1,om] = freqz(b1,a1);
mag1 = 20*log(abs(h1));
ang1 = angle(h1);

[n2,wn2] = cheb1ord(w1,w2,rp,rs);
disp('Order of Chebyshew type 1 Filter');
disp(n2);
disp(wn2);
[b2,a2] = cheby1(n2,rp,wn2,'bandpass');
[h2,om] = freqz(b2,a2);
mag2 = 20*log(abs(h2));
ang2 = angle(h2);

[n3,wn3] = cheb1ord(w1,w2,rp,rs);
disp('Order of Chebyshew type 2 Filter');
disp(n3);
disp(wn3);
[b3,a3] = cheby2(n3,rs,wn3,'bandpass');
[h3,om] = freqz(b3,a3);
mag3 = 20*log(abs(h3));
ang3 = angle(h3);

figure;
subplot(3,1,1);
plot(om/pi,mag1,LineWidth=2);
xlabel('n');
ylabel('magnitude');
title('IIR BANDPASS FILTER(1602-22-735-077)');
subplot(3,1,2);
plot(om/pi,mag2,LineWidth=2);
xlabel('n');
ylabel('magnitude');
title('IIR BANDPASS FILTER(1602-22-735-077)');
subplot(3,1,3);
plot(om/pi,mag3,LineWidth=2);
xlabel('n');
ylabel('magnitude');
title('IIR BANDPASS FILTER(1602-22-735-077)');

24. IIR_STOP
clc;
clear;
close all;
rp = input('Enter the pass band ripple : ');
rs = input('Enter the stop band attenuation : ');
wp = input('Enter the pass band edge frequency : ');
ws = input('Enter the stop band edge frequency : ');
fs = input('Enter the sampling frequency : ');
w1 = 2*wp/fs;
w2 = 2*ws/fs;
[n1,wn1] = buttord(w1,w2,rp,rs);
disp('Order of Butterworth Filter');
disp(n1);
disp(wn1);
[b1,a1] = butter(n1,wn1,'stop');
[h1,om] = freqz(b1,a1);
mag1 = 20*log(abs(h1));
ang1 = angle(h1);

[n2,wn2] = cheb1ord(w1,w2,rp,rs);
disp('Order of Chebyshew type 1 Filter');
disp(n2);
disp(wn2);
[b2,a2] = cheby1(n2,rp,wn2,'stop');
[h2,om] = freqz(b2,a2);
mag2 = 20*log(abs(h2));
ang2 = angle(h2);

[n3,wn3] = cheb1ord(w1,w2,rp,rs);
disp('Order of Chebyshew type 2 Filter');
disp(n3);
disp(wn3);
[b3,a3] = cheby2(n3,rs,wn3,'stop');
[h3,om] = freqz(b3,a3);
mag3 = 20*log(abs(h3));
ang3 = angle(h3);

figure;
subplot(3,1,1);
plot(om/pi,mag1,LineWidth=2);
xlabel('n');
ylabel('magnitude');
title('IIR BAND STOP FILTER(1602-22-735-077)');
subplot(3,1,2);
plot(om/pi,mag2,LineWidth=2);
xlabel('n');
ylabel('magnitude');
title('IIR BAND STOP FILTER(1602-22-735-077)');
subplot(3,1,3);
plot(om/pi,mag3,LineWidth=2);
xlabel('n');
ylabel('magnitude');
title('IIR BAND STOP FILTER(1602-22-735-077)');

25. Decimation
clc;clear;close all;
N= 50;
n = 0:1:N-1;
x=sin((2*pi*n)/20);
a=1;
b=fir1(5,0.5,'low');
y=filter(b,a,x);

M=2;
z=y(1:M:N);
n1=1:1:N/M;
subplot(3,1,1);

stem(n,x);
title('Input signal(1602-22-735-066)');
xlabel('No of samples');
ylabel('Amplitude');
grid on;

subplot(3,1,2);
stem(y)
title('Filteredd Signal (1602-22-735-066)');
xlabel('no. of samples');
ylabel('Amplitude');
grid on;
subplot(3,1,3);
stem(n1-1,z);
title('output Signal (1602-22-735-066)');
xlabel('No. of samples');
ylabel('Amplitude');
grid on;
26. Interpolation
clc;clear;close all;
N= 50;
n = 0:1:N-1;
x=sin((2*pi*n)/20);

L=2;
x1 = zeros(1,(L*N));
n1 = L:1:L*N;
j = 1:L:L*N;
x1(j) = x;

a=1;
b=fir1(5,0.5,'low');
y=filter(b,a,x1);

subplot(3,1,1);

stem(n,x);
title('Input signal(1602-22-735-066)');
xlabel('No of samples');
ylabel('Amplitude');
grid on;

subplot(3,1,2);
stem(n1,x1(1:length(n1)));
title('Interpolated Signal(1602-22-735-066)');
xlabel('no. of samples');
ylabel('Amplitude');
grid on;
subplot(3,1,3);
stem(n1,L*y(1:length(n1)));
title('Filtered Signal (1602-22-735-066)');
xlabel('No. of samples');
ylabel('Amplitude');
grid on;

You might also like