DSP codes
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);
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]);
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");
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');
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');
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
LHS
RHS
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
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));