0% found this document useful (0 votes)
10 views41 pages

DSP codes

The document contains MATLAB code for digital signal processing (DSP) techniques, including sampling theorem, discrete Fourier transform (DFT) calculations, properties of DFT, and implementations of the Radix-2 DIT FFT algorithm. It covers various aspects such as time-domain and frequency-domain signal analysis, linearity, periodicity, circular convolution, and Parseval's theorem. The code includes visualizations for input sequences, magnitude, phase plots, and verification of properties through calculations.
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)
10 views41 pages

DSP codes

The document contains MATLAB code for digital signal processing (DSP) techniques, including sampling theorem, discrete Fourier transform (DFT) calculations, properties of DFT, and implementations of the Radix-2 DIT FFT algorithm. It covers various aspects such as time-domain and frequency-domain signal analysis, linearity, periodicity, circular convolution, and Parseval's theorem. The code includes visualizations for input sequences, magnitude, phase plots, and verification of properties through calculations.
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/ 41

DSP CODES:

I Sampling theorem:
f1 = 20;
f2 = 40;
T=1;

Nyq_freq = 2*max(f1,f2);

Fs_ov = Nyq_freq*2;
Fs_eq = Nyq_freq*1;
Fs_und = Nyq_freq/1.5;

t_eq = 0:(1/Fs_eq):(T-1/Fs_eq);
t_ov = 0:(1/Fs_ov):(T-1/Fs_ov);
t_und = 0:(1/Fs_und):(T-1/Fs_und);

x_t_eq = 2*sin(2*pi*f1*t_eq) + 3*sin(2*pi*f2*t_eq);


x_t_ov = 2*sin(2*pi*f1*t_ov) + 3*sin(2*pi*f2*t_ov);
x_t_und = 2*sin(2*pi*f1*t_und) + 3*sin(2*pi*f2*t_und);

N_eq = length(x_t_eq);
freq_eq = (-Fs_eq/2):(Fs_eq/N_eq):((Fs_eq/2) - (Fs_eq/N_eq));
X_f_eq = fftshift(fft(x_t_eq, N_eq));

N_ov = length(x_t_ov);
freq_ov = (-Fs_ov/2):(Fs_ov/N_ov):((Fs_ov/2) - (Fs_ov/N_ov));
X_f_ov = fftshift(fft(x_t_ov, N_ov));

N_und = length(x_t_und);
freq_und = (-Fs_und/2):(Fs_und/N_und):((Fs_und/2) - (Fs_und/N_und));
X_f_und = fftshift(fft(x_t_und, N_und));

sgtitle("Signal Analysis");

subplot(3,2,1);
stem(t_eq, x_t_eq);
xlabel('Time(s)');
ylabel('Amplitude');
title('Time domain signal (equal)');
subplot(3,2,2);
plot(freq_eq, abs(X_f_eq));
xlabel('Frequency(Hz)');
ylabel('Magnitude');
title('Frequency domain signal (equal)');
xlim([-Fs_eq/2, Fs_eq/2]);

subplot(3,2,3);
stem(t_ov, x_t_ov);
xlabel('Time(s)');
ylabel('Amplitude');
title('Time domain signal (over)');

subplot(3,2,4);
plot(freq_ov, abs(X_f_ov));
xlabel('Frequency(Hz)');
ylabel('Magnitude');
title('Frequency domain signal (over)');
xlim([-Fs_ov/2, Fs_ov/2]);

subplot(3,2,5);
stem(t_und, x_t_und);
xlabel('Time(s)');
ylabel('Amplitude');
title('Time domain signal (under)');

subplot(3,2,6);
plot(freq_und, abs(X_f_und));
xlabel('Frequency(Hz)');
ylabel('Magnitude');
title('Frequency domain signal (under)');
xlim([-Fs_und/2, Fs_und/2]);

II. DFT Codes:

A. Code using in built functions (any input):

x = [1,2,3,4];
N = length(x);
x_dft = zeros(N,1);
n=0:N-1;
x_dft = fft(x);
magnitude = abs(x_dft);
phase = atan2(imag(x_dft), real(x_dft));
disp("dft is: ");
disp(x_dft.');
disp("magnitude is: ");
disp(magnitude.');
disp("phase is: ");
disp(phase.');

x_idft = zeros(N,1);
x_idft = ifft(x_dft);
disp("idft is:");
disp(x_idft.');

subplot(3,2,1);
stem(abs(x));
title("Input sequence")

subplot(3,2,2);
stem(n, abs(x_dft));
title("Magnitude");

subplot(3,2,3);
stem(n, phase);
title("Phase");

subplot(3,2,4);
stem(abs(x_idft));
title("Idft signal");

B. Code without using in built functions (any input):

x = [1,2+2i,3,2-2i];
N = length(x);
X = zeros(N,1);
for k=0:(N-1)
for n=0:(N-1)
X(k+1)=X(k+1) + x(n+1)*exp(1i*2*pi*n*k/N);
end
end
disp("DFT is: ")
X
n = 0:N-1;
subplot(3,2,1);
stem(abs(x));
title('Input sequence');
xlabel('n');
ylabel('amplitude');
subplot(3,2,2);
stem(n , X);
title('Magnitude plot');
xlabel('Frequency(Hz)');
ylabel('magnitude');
subplot(3,2,3);
stem(n , atan2(imag(X), real(X)));
title('Phase plot');
xlabel('Frequency(Hz)');
ylabel('phase');
Y=zeros(N,1);
for n=0:N-1
for k=0:N-1
Y(n+1) = Y(n+1) + X(k+1)*(1/N)*exp(-1i*2*pi*n*k/N);
end
end
disp("Idft is: ");
Y
subplot(3,2,4);
stem(abs(Y));
title('Idft signal');
xlabel('n');
ylabel('amplitude');

III Properties of DFT:


1. Linearity:
% Create two sequences
x1 = [7, 6, 2, 0];
x2 = [7, 6, 2, 0];
N = length(x1);
% Compute the DFT of the linear combination of sequences
X1 = zeros(1, N);
X2 = zeros(1, N);
X_linear = zeros(1, N);
for k = 0:N-1
for n = 0:N-1
X1(k+1) = X1(k+1) + x1(n+1) * exp(-1j*2*pi*k*n/N);
X2(k+1) = X2(k+1) + x2(n+1) * exp(-1j*2*pi*k*n/N);
X_linear(k+1) = X_linear(k+1) + (x1(n+1) + x2(n+1)) * exp(-
1j*2*pi*k*n/N);
end
end
X1
X2
X1+X2
X_linear
% Check if the linearity property holds
linearity_check = isequal((X1 + X2), X_linear);
linearity_check
% Display the result
if linearity_check
disp('a) Linearity property is verified.');
else
disp('a) Linearity property is NOT verified.');
end
subplot(3,2,1);
stem(x1);
title('x1');
subplot(3,2,2);
stem(x2);
title('x2');
subplot(3,2,3);
stem(X1);
title('X1');
subplot(3,2,4);
stem(X2);
title('X2');
subplot(3,2,5);
stem(X1+X2);
title('X1+X2');
subplot(3,2,6);
stem(X_linear);
title('X_linear');

2. Periodicity:
% Define a sequence
y1 = [4, 5, 6, 7];
% Create a periodic sequence
N = length(y1);
y_periodic = zeros(1, N);
for n = 0:N-1
y_periodic(n+1) = y1(mod(n, N) + 1);
end
% Compute the DFT of the periodic sequence
Y_periodic = zeros(1, N);
Y1 = zeros(1,N);
for k = 0:N-1
for n = 0:N-1
Y1(k+1) = Y1(k+1) + y1(n+1) * exp(-1j*2*pi*k*n/N);
Y_periodic(k+1) = Y_periodic(k+1) + y_periodic(n+1) * exp(-
1j*2*pi*k*n/N);
end
end
y1
y_periodic
Y1
Y_periodic
% Check if the periodicity property holds
periodicity_check = isequal(Y1, Y_periodic);
% Display the result
if periodicity_check
disp('b) Periodicity property is verified.');
else
disp('b) Periodicity property is NOT verified.');
end
subplot(3,2,1);
stem(y1);
title('y1');
subplot(3,2,2);
stem(y_periodic);
title('y_periodic');
subplot(3,2,3);
stem(Y1);
title('Y1');
subplot(3,2,4);
stem(Y_periodic);
title('Y_periodic');

3. Circular Time Shift:


x = [4, -4, 5, -5];
N = length(x);
% Create a circularly shifted sequence
m = 2; % Shift amount
x_shifted = zeros(1, N);
for n = 0:N-1
x_shifted(n+1) = x(mod(n - m, N) + 1);
end
% Compute the DFT of the shifted sequence
X_shifted_dft = zeros(1,N);
X_dft = zeros(1,N);
for k = 0:N-1
for n = 0:N-1
X_dft(k+1) = X_dft(k+1) + x(n+1) * exp(-1i*2*pi*k*n/N);
X_shifted_dft(k+1) = X_shifted_dft(k+1) + x_shifted(n+1) * exp(-
1i*2*pi*k*n/N);
end
end
X = zeros(1,N);
X = (exp(-1i*2*pi*m*k/N)) .* (X_dft);
x
x_shifted
X_dft
X
X_shifted_dft
% Check if the circular shift property holds
circular_shift_check = isequal(X_shifted_dft, X);
% Display the result
if circular_shift_check
disp('c) Circular shift property is verified.');
else
disp('c) Circular shift property is NOT verified.');
end
subplot(3,2,1);
stem(x);
title('x');
subplot(3,2,2);
stem(x_shifted);
title('x-shifted');

subplot(3,2,3);
stem(X_dft);
title('X-dft');
subplot(3,2,4);
stem(X_shifted_dft);
title('X-shifted-dft');
subplot(3,2,5);
stem(X);
title('X_dft * W^km');
4. Circular Frequency Shift:
y = [4, 5, 6, 7];
y
N = length(y);
l = 3;
n=0:N-1;
Y_dft = fft(y);
Y_dft
Y_fshift = circshift(Y_dft, l);
Y_fshift
LHS = ifft(Y_fshift);
LHS
RHS = exp(j*2*pi*l*n/N) .* y;
RHS
f_shift = isequal(LHS, RHS);
if f_shift
disp('c) Circular frequency shift property is verified.');
else
disp('c) Circular frequency shift property is NOT verified.');
end
subplot(3,2,1);
stem(y);
title('y');
subplot(3,2,2);
stem(Y_dft);
title('Y-dft');
subplot(3,2,3);
stem(Y_fshift);
title('Y-fshift');
subplot(3,2,4);
stem(LHS);
title('LHS');
subplot(3,2,5);
stem(RHS);
title('RHS');

5. Conjugate Symmetry:
clc
clear all
close all
N=4; %period
n=1:N;
k=1:N;
x(n) = [4, 5, 6, 7];
% Getting LHS of theorem:
X(k) = fft(conj(x));
LHS = X;
LHS
subplot(2,2,1)
stem(n,abs(LHS))
title("Magnitude of LHS")
ylabel('Amplitude ---->');
xlabel('K ---->');
subplot(2,2,2)
stem(n,angle(LHS))
title("Phase of LHS")
ylabel('Angle ---->');
xlabel('K ---->');
% Getting RHS of theorem:
K=2:N;
RHS = X(1);
for k=2:N
RHS(k) = fft(conj(X(N+2-k)));
end
RHS
subplot(2,2,3)
stem(n,abs(RHS))
title("Magnitude of RHS")
ylabel('Amplitude ---->');
xlabel('K ---->');
subplot(2,2,4)
stem(n,angle(RHS))
title("Phase of RHS")
ylabel('Angle ---->');
xlabel('K ---->');

6. Circular Convolution:
clc
clear all
close all
x1 = [4, 5, 6];
x2 = [7, 8, 9, 10];
if length(x1)>length(x2)
N = length(x1)
else
N = length(x2)
end
% Making length of 2 sequences equal
x1 = [x1 zeros(1,N-length(x1))]
x2 = [x2 zeros(1,N-length(x2))]
% Calculating DFTs of 2 sequences
X1 = fft(x1);
X2 = fft(x2);
X1
X2
% Calculating circular convolution
cirCon = ifft(X1.*X2)
cirCon
stem(cirCon)
ylabel('Amplitude ---->')
xlabel('n ---->')

7. Time Reversal:
clc;
close all;
clear all;
xn=[4, 5, 6, 7];
N=length(xn);
Xk=fft(xn);
Xk
n=0:N-1;
x1n=xn(mod(-n,N)+1); %Time reversing original sequence
LHS=fft(x1n);
RHS=conj(Xk);
x1n
LHS
RHS
%Plotting
subplot(221);
stem(abs(LHS));
ylabel('Amplitude ---->');
xlabel('K ---->');
title('Magnitude of LHS');
subplot(222);
stem(angle(LHS));
ylabel('Angle ---->');
xlabel('K ---->');
title('Phase of LHS');
subplot(223);
stem(abs(RHS));
ylabel('Amplitude ---->');
xlabel('K ---->');
title('Magnitude of RHS');
subplot(224);
stem(angle(RHS));
ylabel('Angle ---->');
xlabel('K ---->');
title('Phase of RHS');

8. Parseval’s theorem:
clc
clear all
close all

% Original signal
x = [1,2,3,4,5];
x
% Finding X(k)
X = fft(x);
X

% Finding LHS and RHS


LHS = sum(x.^2)
RHS = sum(abs(X).^2)/length(x) %N = length(x);

LHS
RHS

III Radix-2 DIT FFT Algorithm:


1. Dit fft 8 point dft:

dit fft
x=[1, 2, 3, 4, 4, 3, 2, 1];
x=[x,zeros(1,(N-length(x)))];
N=length(x);

y=DIT_R2(x);
disp("Radix-2 DIT FFT method result");
y

X=fft(x,N);
disp("direct computation result")
X

k=0:N-1;
subplot(2,2,1)
stem(abs(y));
xlabel("k")
ylabel("|X(K)|")
title("radix-2 DIT FFT magnitude plot")

subplot(2,2,2)
stem(angle(y));
xlabel("k")
ylabel("<X(K)")
title("radix-2 DIT FFT phase plot")

subplot(2,2,3)
stem(abs(X));
xlabel("k")
ylabel("|X(K)|")
title("Direct computation magnitude plot")

subplot(2,2,4)
stem(angle(X));
xlabel("k")
ylabel("<X(K)")
title("Direct computation phase plot")

function y=DIT_R2(x)
p=nextpow2(length(x));
x=[x,zeros(1,(2^p)-length(x))];
N=length(x);
s=log2(N);
half=1;
x=bitrevorder(x);
disp(x);
disp(s);

for stage=1:s
for index=0:(2^stage):(N-1)
for n=0:half-1
pos=n+index+1;
pow=(2^(s-stage))*n;
w=exp(-1i*2*pi*pow/N);
a=x(pos)+x(pos+half).*w;
b=x(pos)-x(pos+half).*w;
x(pos)=a;
x(pos+half)=b;
disp(x(pos));
disp(x(pos+half));
end
end
half=2*half;
end
y=x;
end
2. Dit fft 4 point dft:

x=[1, 2, 3, 4];
%x=[x,zeros(1,(N-length(x)))];
N=length(x);

y=DIT_R2(x);
disp("Radix-2 DIT FFT method result");
y

X=fft(x,N);
disp("direct computation result")
X

k=0:N-1;
subplot(2,2,1)
stem(abs(y));
xlabel("k")
ylabel("|X(K)|")
title("radix-2 DIT FFT magnitude plot")

subplot(2,2,2)
stem(angle(y));
xlabel("k")
ylabel("<X(K)")
title("radix-2 DIT FFT phase plot")

subplot(2,2,3)
stem(abs(X));
xlabel("k")
ylabel("|X(K)|")
title("Direct computation magnitude plot")

subplot(2,2,4)
stem(angle(X));
xlabel("k")
ylabel("<X(K)")
title("Direct computation phase plot")

function y=DIT_R2(x)
p=nextpow2(length(x));
x=[x,zeros(1,(2^p)-length(x))];
N=length(x);
s=log2(N);
half=1;
x=bitrevorder(x);
disp(x);
disp(s);

for stage=1:s
for index=0:(2^stage):(N-1)
for n=0:half-1
pos=n+index+1;
pow=(2^(s-stage))*n;
w=exp(-1i*2*pi*pow/N);
a=x(pos)+x(pos+half).*w;
b=x(pos)-x(pos+half).*w;
x(pos)=a;
x(pos+half)=b;
disp(x(pos));
disp(x(pos+half));
end
end
half=2*half;
end
y=x;
end

3. DIT FFT IDFT 4 point:


dit idft:
X = [9, -5, 5, -1];
N = length(X);
k = 0:N-1;
C_X = conj(X);
y = DIT_R2(C_X);
y = conj(y)/4;
y
x_n = ifft(X,N);
x_n
k = 0:N-1;
subplot(2,2,1);
stem(abs(y));
title("Radix-2 DIT FFT idft");
subplot(2,2,2);
stem(angle(y));
title("Phase of idft");
subplot(2,2,3);
stem(abs(x_n));
title("Direct idft");
subplot(2,2,4);
stem(angle(x_n));
title("Phase of direct idft");
function y = DIT_R2(x)
p = nextpow2(length(x));
x = [x, zeros(1, (2^p)-length(x))];
N = length(x);
s = log2(N);
half = 1;
x = bitrevorder(x);
disp(x);
disp(s);
for stage = 1:s
for index = 0:(2^stage):N-1
for n = 0:(half-1)
pos = n+index+1;
pow = (2^(s-stage))*n;
w = exp(-1i*2*pi*pow/N);
a = x(pos) + (x(pos+half).*w);
b = x(pos) - (x(pos+half).*w);
x(pos) = a;
x(pos+half) = b;
disp(x(pos));
disp(x(pos+half));
end
end
half = 2*half;
end
y = x;
end

IV Radix-2 DIF FFT Algorithm:

1. DFT

x = [1,2,3,4];
N = length(x);
k= 0:N-1;
p = nextpow2(length(x));
x = [x, zeros(1, (2^p)-length(x))];
N = length(x);
y = DIF_R2(x);
y
X = fft(x,N);
X
k = 0:N-1;
subplot(2,2,1);
stem(abs(y));
title("8point dif dft magnitude");
subplot(2,2,2);
stem(angle(y));
title("8point dif dft phase");
subplot(2,2,3);
stem(abs(X));
title("direct dft magnitude");
subplot(2,2,4);
stem(angle(X));
title("direct dft phase");
function y=DIF_R2(x)
p = nextpow2(length(x));
x = [x, zeros(1, (2^p)-length(x))];
N = length(x);
s = log2(N);
half = N/2;
s
for stage = 1:s
for index = 0:(N/(2^(stage-1))):(N-1)
for n=0:(half-1)
pos = n+index+1;
pow = (2^(stage-1))*n;
w = exp(-1i*2*pi*pow/N);
a = x(pos) + x(pos+half);
b = (x(pos) - x(pos+half)).*w;
x(pos) = a;
x(pos+half) = b;
disp(x(pos));
disp(x(pos+half));
end
end
half = half/2;
end
y = bitrevorder(x);
end
2 IDFT
X = [10, 1+1i, 0, 1-1i];
N = length(X);
k= 0:N-1;
C_X = conj(X);
z = DIF_R2(C_X);
y = conj(z)./N;
y
x = ifft(X,N);
x
k = 0:N-1;
subplot(2,2,1);
stem(abs(y));
title("dif idft magnitude");
subplot(2,2,2);
stem(angle(y));
title("dif idft phase");
subplot(2,2,3);
stem(abs(x));
title("direct idft magnitude");
subplot(2,2,4);
stem(angle(x));
title("direct idft phase");
function y=DIF_R2(x)
p = nextpow2(length(x));
x = [x, zeros(1, (2^p)-length(x))];
N = length(x);
s = log2(N);
half = N/2;
s
for stage = 1:s
for index = 0:(N/(2^(stage-1))):(N-1)
for n=0:(half-1)
pos = n+index+1;
pow = (2^(stage-1))*n;
w = exp(-1i*2*pi*pow/N);
a = x(pos) + x(pos+half);
b = (x(pos) - x(pos+half)).*w;
x(pos) = a;
x(pos+half) = b;
disp(x(pos));
disp(x(pos+half));
end
end
half = half/2;
end
y = bitrevorder(x);
end

V. Hardware Experiments:
1 DFT
#include<stdio.h>
#include<math.h>
int main(void) {
int N,k,n,i;
float pi=3.1416,sumre=0,sumim=0,Xr[8]={0,0},Xi[8]={0,0};
int x[32];
printf("Enter the length of sequence\n");
scanf("%d", &N);
printf("Enter the sequence\n");
for(i=0;i<N;i++)
{
scanf("%d",&x[i]);
}
for(k=0;k<N;k++){
sumre=0;
sumim=0;
for(n=0;n<N;n++){
sumre=sumre+x[n]*cos(2*pi*k*n/N);
sumim=sumim-x[n]*sin(2*pi*k*n/N);
}
Xr[k]=sumre;
Xi[k]=sumim;
printf("x([%d])=\t%f\t+\t%fi\n",k,Xr[k],Xi[k]);
}
return 0;
}

2 IDFT
#include<stdio.h>
#include<math.h>
int main(void) {
int N,k,n,i;
float pi=3.1416, sumre=0, sumim=0, Xr[8] = {0,0}, Xi[8]={0,0};
float x1[4];
float x2[4];
printf("Enter the length of sequence\n");
scanf("%d", &N);
printf("Enter the real part\n");
for(i=0;i<N;i++){
scanf("%f", &x1[i]);
}
printf("Enter the imaginary part\n");
for (i=0; i<N;i++){
scanf ("%f", &x2[i]);
}
For (k=0; k<N;k++){
sumre=0;
sumim=0;
for(n=0;n<N;n++){
sumre=sumre+x1[n]*cos(2*pi*k*n/N)-x2[n]*sin(2*pi*k*n/N);
sumim=sumim+x2[n]*cos(2*pi*k*n/N)+x1[n]*sin(2*pi*k*n/N);
}
Xr[k]=sumre/N;
Xi[k]=sumim/N;
printf("x([%d])=\t%f\t+\t%fi\n",k,Xr[k],Xi[k]);
}
return 0;
}
3 Linear Convolution
#include <stdio.h>
int x[20], h[20], y[20];
int main()
{
int i, j, m, n;
printf("Enter length of x(n): ");
scanf("%d", &m); // Getting Length of x(n)
printf("\nEnter the length of h(n): ");
scanf("%d", &n); // Getting Length of h(n)
printf("\nEnter x(n): ");
for (i = 0; i < m; i++)
{
scanf("%d", &x[i]); // Getting x(n)
}
printf("\nEnter h(n): ");
for (i = 0; i < n; i++)
{
scanf("%d", &h[i]); // Getting h(n)
}
/*padding zeros */
for (i = m; i <= m + n - 1; i++)
{ x[i]=0;
}
for (i = n; i <= m + n - 1; i++)
{ h[i]=0;
}
for (i = 0; i < m + n - 1; i++)
{
y[i] = 0;
for (j = 0; j <= i; j++)
{
y[i] = y[i] + (x[j] * h[i - j]); // Calculating Linear Convolution
}
}
//displaying the o/p
for (i = 0; i < m + n - 1; i++)
{
printf("The Value of output y[%d]=%d\n", i, y[i]); // Printing it to
STDOUT
}
printf("\n\n");
}
4 Circular Convolution
#include <stdio.h>
int m, n, x[30], h[30], y[30], i, j, k, x2[30], a[30];
void main()
{
printf("Enter the length of the first sequence: ");
scanf("%d", &m);
printf("Enter the length of the second sequence: ");
scanf("%d", &n);
printf("Enter the first sequence: ");
for (i = 0; i < m; i++)
scanf("%d", &x[i]);
printf("Enter the second sequence: ");
for (j = 0; j < n; j++)
scanf("%d", &h[j]);
if (m - n != 0)
{
if (m > n)
{
for (i = n; i < m; i++)
h[i] = 0;
n = m;
}
for (i = m; i < n; i++)
x[i] = 0;
m = n;
}
y[0] = 0;
a[0] = h[0];
for (j = 1; j < n; j++)
a[j] = h[n - j];
/*Circular convolution*/
for (i = 0; i < n; i++)
y[0] += x[i] * a[i];
for (k = 1; k < n; k++)
{
y[k] = 0;
for (j = 1; j < n; j++)
x2[j] = a[j - 1];
x2[0] = a[n - 1];
for (i = 0; i < n; i++)
{
a[i] = x2[i];
y[k] += x[i] * x2[i];
}
}
printf("Circular convolution of x(n) and h(n) is:\n");
for (i = 0; i < n; i++)
printf("y[%d]=\t%d\n", i, y[i]);
}
VI IIR FILTERS
1 Butterworth
digital_pass = input("enter the pass band edge frequency: ");
digital_stop = input("enter the stop band edge frequency: ");
n=input("Enter 1 for IIT\nEnter 2 for BLT\n");
if (n==1)
T=1;
analog_pass = digital_pass/T;
analog_stop = digital_stop/T;
else
T=2;
analog_pass = (2/T)*tan(digital_pass/2);
analog_stop = (2/T)*tan(digital_stop/2);
end
choice = input("Enter 1 for gain in rad\n Enter 2 for gain in db");
if (choice==1)
Ap = input("Enter the pass band gain: \n");
As = input("Enter the stop band gain: \n");
a = (1/(As*As))-1;
b = (1/(Ap*Ap))-1;
else
Ap = input("Enter the pass band gain: \n");
As = input("Enter the stop band gain: \n");
a = 10^(0.1*As)-1;
b = 10^(0.1*Ap)-1;
end
num1 = log(a/b);
disp(num1);
den1 = 2*log(analog_stop/analog_pass);
disp(den1);
N = num1/den1
N1 = fix(N);
if(N==N1)
No=N1;
else
No=N1+1;
end
cs=analog_stop/(a^(1/(2*No)));
cp=analog_pass/(b^(1/(2*No)));
c=(cp+cs)/2
[Bn,An]=butter(No,1,'s');
Hsn=tf(Bn,An);
Hsn
[B,A]=butter(No,c,'s');
Hs=tf(B,A);
Hs
if(n==1)
[num,den]=impinvar(B,A,1/T);
else
[num,den]=bilinear(B,A,1/T);
end
Hz=tf(num,den,T);
Hz
W=0:pi/180:pi;
Hw=freqz(num,den,W);
Hw_mag=abs(Hw);
Hw_phase = angle(Hw);
figure;
subplot(2,1,1);
plot(W/pi,Hw_mag);
title("mag response");
subplot(2,1,2);
plot(W/pi,Hw_phase);
title("Phase response");

2 Chebyshev
digital_pass=input("Enter the pass band edge frequency: \n");
digital_stop=input("Enter the stop band edge frequency: \n");
n=input("Enter 1 for IIT\n Enter 2 for BLT\n");
if(n==1)
T=1;
analog_pass=digital_pass/T;
analog_stop=digital_stop/T;
else
T=2;
analog_pass=(2/T)*tan(digital_pass/2);
analog_stop=(2/T)*tan(digital_stop/2);
end
% To determine normalised specifications
analog_pass_prime=analog_pass/analog_pass;
analog_stop_prime=analog_stop/analog_pass;
choice=input("Enter 1 for gain in ratio \n Enter 2 for gain in db\n");
if(choice==1)
Ap=input("Enter the pass band gain : \n");
As=input("Enter the stop band gain : \n");
Ap=abs(mag2db(Ap));
As=abs(mag2db(As));
else
Ap=input("Enter the pass band attenuation : \n");
As=input("Enter the stop band attenuation : \n");
end
%To determine order
[N,CF]=cheb1ord(analog_pass,analog_stop,Ap,As,'s');
N
CF
[Bn,An]=cheby1(N,Ap,1,'s');
Hsn=tf(Bn,An)
[B,A]=cheby1(N,Ap,CF,'s');
hs=tf(B,A)
if(choice==1)
[num,den]=impinvar(B,A,1/T);
else
[num,den]=bilinear(B,A,1/T);
end
Hz=tf(num,den,T)
W=(0:pi/180:pi);
Hw=freqz(num,den,W);
Hw_mag=(abs(Hw));
Hw_phase=angle(Hw);
figure;
subplot(2,1,1);
plot(W/pi,Hw_mag);
title("mag response");
subplot(2,1,2);
plot(W/pi,Hw_phase);
title("Phase response");
VII FIR FILTERS:
1 all windows tables:
n=input('Enter the length of the sequence:');
t=(n-1)/2;
wc=input("Ënter cutoff frequemcy:");
for i=1:n
if i~=t+1
hd(i)= (sin((i-1-t)wc))/(3.1416(i-1-t));
end
if i==t+1
hd(i)=wc*0.3183;
end
end
N=input('Choose among the following windows:\n 1. Rectangular\n 2.
Hanning\n 3. Hamming\n:');
if N==1
for i=1:n
w(i)=1;
end
end
if N==2
for i=1:n
w(i)=0.5*(1-((cos(2*3.1416*(i-1)/(n-1)))));
end
end
if N==3
for i=1:n
w(i)=0.54-(0.46*(cos(2*3.1416*(i-1)/(n-1))))
end
end
for i=1:n
h(i)=hd(i)*w(i)
end
disp(h(i));

2 Hanning LPF without inbuilt functions


N = input("Enter the length: ");
wc = input("Enter the cutoff frequency(radians): ");
tou = (N-1)/2;
hd = zeros(1,N);
wn = zeros(1,N);
whan = zeros(1,N);
for n = 1 : N
whan(n) = 0.5*(1 - cos((2*pi*(n-1))/(N-1)));
if n == tou+1
hd(n) = wc/pi;
wn(n) = hd(n) * whan(n);
else
hd(n) = sin(wc*(n-tou-1))/(pi*(n-tou-1));
wn(n) = hd(n) * whan(n);
end
end
Hz = tf(wn,1,1/2000,'variable','z^-1')
W = 0:pi / 180:pi;
Hw = freqz(wn,1,W);
subplot(2,1,1);
plot(W,abs(Hw));
title("Magnitude Response");
xlabel("w");
ylabel("|H(w)|");
subplot(2,1,2);
plot(W/pi,angle(Hw));
title("Phase Response");
xlabel("w");
ylabel("<H(w)");

3 Hanning LPF using inbuilt functions


fs = 2000;
n = input("Enter length");
choice = input("Select An option for which Filter Type\n1. Low pass filter\n2.
High pass \n");
Wc = input('Enter the Cuttoff Frequency: ');
Wc = Wc / (fs / 2);
switch choice
case 1
ans = fir1(n, Wc, "low", hann(n+1));
freqz(ans, 1, 1024);
case 2
ans = fir1(n-1, Wc, "high", hann(n));
freqz(ans, 1, 1024);
end
4 Hamming LPF without inbuilt functions
N = input("Enter the length: ");
wc = input("Enter the cutoff frequency(radians): ");
tou = (N-1)/2;
hd = zeros(1,N);
wn = zeros(1,N);
wham = zeros(1,N);
for n = 1 : N
wham(n) = 0.54 - 0.46*cos((2*pi*(n-1))/(N-1));
if n == tou+1
hd(n) = wc/pi;
wn(n) = hd(n) * wham(n);
else
hd(n) = sin(wc*(n-tou-1))/(pi*(n-tou-1));
wn(n) = hd(n) * wham(n);
end
end
Hz = tf(wn,1,1/2000,'variable','z^-1')
W = 0:pi / 180:pi;
Hw = freqz(wn,1,W);
figure;
subplot(2,1,1);
plot(W,abs(Hw));
title("Magnitude Response");
subplot(2,1,2);
plot(W/pi,angle(Hw));
title("Phase Response");
5 Hamming LPF using inbuilt functions
fs = 2000;
n = input("Enter length");
choice = input("Select An option for which Filter Type\n1. Low pass filter\n2.
High pass \n");
Wc = input('Enter the Cuttoff Frequency: ');
Wc = Wc / (fs / 2);
switch choice
case 1
ans = fir1(n, Wc, "low", hamming(n+1));
freqz(ans, 1, 1024);
case 2
ans = fir1(n-1, Wc, "high", hamming(n));
freqz(ans, 1, 1024);
end
6 Hanning BPF and BRF using inbuilt functions
fs=2000;
n=input("enter the length: \n");
wc1=input("enter lower cutoff frequency: \n");
wc2=input("enter higher cutoff frequency: \n");
wc1 = wc1/(fs/2);
wc2 = wc2/(fs/2);
bandpass = fir1(n,[wc1,wc2],hann(n+1));
figure;
freqz(bandpass,1,1024);
figure;
impz(bandpass);
bandstop = fir1(n-1,[wc1,wc2],"stop",hann(n));
figure;
freqz(bandstop,1,1024);
figure;
impz(bandstop);

7 Hanning BPF and BRF without inbuilt function


% Bandpass filter using Hanning window
fs = 2000;
n = input("Enter the filter length: \n");
wc1 = input("Enter lower cutoff frequency: \n");
wc2 = input("Enter higher cutoff frequency: \n");
wc1_normalized = wc1 / (fs/2);
wc2_normalized = wc2 / (fs/2);
% Design Hanning window
hanning_window = 0.5 - 0.5 * cos(2 * pi * (0:n) / n);
% Design bandpass filter
bandpass_filter = 2 * (wc2_normalized - wc1_normalized) * sinc(2 *
(wc2_normalized - wc1_normalized) * (-n/2:n/2)) .* hanning_window;
% Design bandstop filter
bandstop_filter = sinc(2 * wc1_normalized * (-n/2:n/2)) .* hanning_window -
sinc(2 * wc2_normalized * (-n/2:n/2)) .* hanning_window;
% Plot
figure;
subplot(2,1,1);
freqz(bandpass_filter, 1, 1024, fs);
title('Bandpass Filter Frequency Response');
subplot(2,1,2);
stem(0:n, bandpass_filter);
title('Bandpass Filter Impulse Response');
figure;
subplot(2,1,1);
freqz(bandstop_filter, 1, 1024, fs);
title('Bandstop Filter Frequency Response');
subplot(2,1,2);
stem(0:n, bandstop_filter);
title('Bandstop Filter Impulse Response');

8 Hamming BPF and BRF using inbuilt function


fs=2000;
n=input("enter the length: \n");
wc1=input("enter lower cutoff frequency: \n");
wc2=input("enter higher cutoff frequency: \n");
wc1 = wc1/(fs/2);
wc2 = wc2/(fs/2);
bandpass = fir1(n,[wc1,wc2],hamming(n+1));
figure;
freqz(bandpass,1,1024);
figure;
impz(bandpass);
bandstop = fir1(n-1,[wc1,wc2],"stop",hamming(n));
figure;
freqz(bandstop,1,1024);
figure;
impz(bandstop);
9 Hamming BPF and BRF without using inbuilt functions
% Bandpass filter using Hamming window
fs = 2000;
n = input("Enter the filter length: \n");
wc1 = input("Enter lower cutoff frequency: \n");
wc2 = input("Enter higher cutoff frequency: \n");
wc1_normalized = wc1 / (fs/2);
wc2_normalized = wc2 / (fs/2);
% Design Hamming window
hamming_window = 0.54 - 0.46 * cos(2 * pi * (0:n) / n);
% Design bandpass filter
bandpass_filter = 2 * (wc2_normalized - wc1_normalized) * sinc(2 *
(wc2_normalized - wc1_normalized) * (-n/2:n/2)) .* hamming_window;
% Design bandstop filter
bandstop_filter = sinc(2 * wc1_normalized * (-n/2:n/2)) .* hamming_window -
sinc(2 * wc2_normalized * (-n/2:n/2)) .* hamming_window;
% Plot
figure;
subplot(2,1,1);
freqz(bandpass_filter, 1, 1024, fs);
title('Bandpass Filter Frequency Response');
subplot(2,1,2);
stem(0:n, bandpass_filter);
title('Bandpass Filter Impulse Response');
figure;
subplot(2,1,1);
freqz(bandstop_filter, 1, 1024, fs);
title('Bandstop Filter Frequency Response');
subplot(2,1,2);
stem(0:n, bandstop_filter);
title('Bandstop Filter Impulse Response');

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy