0% found this document useful (0 votes)
23 views12 pages

1.a) Linear Convolution

The document contains code snippets demonstrating various digital signal processing techniques including linear and circular convolution, auto correlation, cross correlation, discrete Fourier transform (DFT), inverse discrete Fourier transform (IDFT), and summing of sine waves. The code examples compute linear and circular convolution of input sequences, auto correlation and cross correlation of signals, perform DFT and IDFT on sequences, implement an 8-point DFT using a DIF algorithm, and sum two sine waves with different frequencies before analyzing the signal in the frequency domain.

Uploaded by

Ram
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)
23 views12 pages

1.a) Linear Convolution

The document contains code snippets demonstrating various digital signal processing techniques including linear and circular convolution, auto correlation, cross correlation, discrete Fourier transform (DFT), inverse discrete Fourier transform (IDFT), and summing of sine waves. The code examples compute linear and circular convolution of input sequences, auto correlation and cross correlation of signals, perform DFT and IDFT on sequences, implement an 8-point DFT using a DIF algorithm, and sum two sine waves with different frequencies before analyzing the signal in the frequency domain.

Uploaded by

Ram
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/ 12

1.

a) Linear convolution :
#include<stdio.h>
int x[15],h[15],y[15];
main()
{
int i,j,m,n;
printf("Enter the range of m");
scanf("%d",&m);
printf("Enter the range of n");
scanf("%d",&n);
printf("Enter the values of x(n):");
for(i=0;i<m;i++)
{
scanf("%d",&x[i]);
}
printf("Enter the values of h(n):");
for(i=0;i<n; i++)
{
scanf("%d",&h[i]);
}
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]);
}
}
for(i=0;i<m+n-1;i++)
{
printf("\n The Value of output y[%d]=%d",i,y[i]);
}
}

Output:

Enter the range of m3


Enter the range of n4
Enter the values of x(n):-6
5
8
Enter the values of h(n):1
7
4
3
The Value of output y[0]=-6
The Value of output y[1]=-37
The Value of output y[2]=19
The Value of output y[3]=58
The Value of output y[4]=47
The Value of output y[5]=24
1b)
Circular convolution:
#include<stdio.h>
int x[15],h[15],y[15],z[15];
main()
{
int i,j,m,n;
printf("\n enter value for m");
scanf("%d",&m);
printf("\n enter value for n");
scanf("%d",&n);
printf("Enter values for i/p x(n):\n");
for(i=0; i<m; i++)
scanf("%d",&x[i]);
printf("Enter Values for i/p h(n) \n");
for(i=0; i<n; i++)
scanf("%d",&h[i]);
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]);
}
}

printf("\n The Value of linear convolution");


for(i=0; i<m+n-1; i++)
printf("\ny[%d]=%d",i,y[i]);

for(i=0; i<=m+n-1; i++)


z[i]=0;
if (m>n)
{
printf("\n The Value of circular convolution");
for(i=0;i<m;i++)
{
z[i]=y[i]+y[m+i];
printf("\nz[%d]=%d",i,z[i]);
}
}
else
{
printf("\n The Value of circular convolution");
for(i=0;i<n;i++)
{
z[i]=y[i]+y[n+i];
printf("\nz[%d]=%d",i,z[i]);
}
}
}
2a)Auto correlation:
#include<stdio.h>
int x[15],y[15],z[15];
int main()
{
int n,k,i,j,m,sum=0;

printf("Enter the range of m");


scanf("%d",&m);

printf("Enter the values of x(n):");


for(i=0;i<m;i++)
scanf("%d",&x[i]);

for(i=m;i<(2*m)-1;i++)
x[i]=0;

// X1: 1 X2: 1 X3: 2 X4: 1 X5: 0 X6: 0 X7: 0


for(n=(1-m);n<m;n++)
{
for (j=(1-m);j<m;j++)
y[j]=0;

for (j=0;j<(2*m)-1;j++)
y[j+n]=x[j];

for (k=0;k<(2*m)-1;k++)
sum = sum + (x[k]*y[k]) ;

z[n]=sum;
printf("\n The Value of output z[%d]=%d",n,z[n]);
sum=0;
}
}
2b)
Cross correlation:
#include<stdio.h>
int x[15],y[15],z[15],r[15];
int main(){
int n,k,i,j,m,sample,sum=0;
printf("Enter the range of m for x(m):");
scanf("%d",&m);
printf("Enter the range of n for y(n):");
scanf("%d",&n);
for(i=0;i<=m+n-1;i++)
x[i]=0;
printf("Enter the values of x(m):");
for(i=0;i<m;i++)
scanf("%d",&x[i]);
printf("Enter the values of y(n):");
for(j=0;j<n;j++)
scanf("%d",&y[j]);
for(sample=1-n;sample<m;sample++){
for (i=0;i<m+n-1;i++)
z[i]=0;
for (j=0;j<n;j++)
z[j+sample]=y[j];
for (k=0;k<(m+n);k++)
sum=sum+(x[k]*z[k]) ;
r[1-n]=sum;
printf("\n The Value of output z[%d]=%d",sample,r[1-n]);
sum=0;
}
}
3) DFT
#include<stdio.h>
#include<math.h>
float x[10],Xr[10],Xi[10],psd;
int k,n,N;
void main()
{
printf("Enter size N\n");
scanf("%d",&N);
printf("Enter input sequence \n");
for(n=0;n<N;n++)
scanf("%f",&x[n]);
for(k=0;k<N;k++)
{
Xr[k]=0;
Xi[k]=0;
for(n=0;n<N;n++)
{
Xr[k]=(x[n]*cos((6.28*k*n)/N))+Xr[k];
Xi[k]=(-(x[n]*sin((6.28*k*n)/N)))+Xi[k];
}
printf("\n%.3f+(%.3fi)",Xr[k],Xi[k]);
}
psd=0;
for(k=0;k<N;k++)
psd=((Xr[k]*Xr[k])+(Xi[k]*Xi[k]))+psd;
psd=psd/N;
printf("\n power density spectrum %.3f",psd);
}

Output:

Enter size N
4
Enter input sequence
6
-5
4
-3

2.000+(0.000i)
2.003+(1.994i)
18.000+(0.035i)
1.990+(-2.019i)
power density spectrum 0.000
4)C- program to compute 8 – FFT of given sequences using DIF – FFT algorithm
#include<stdio.h>
#include<math.h>
#define pi 3.14
float x[10],a[10],br[10],bi[10],Xr[10],Xi[10];
float p,q,r,s,Mag[10],pha[10];
int i;
void main()
{printf("enter input sequence x:\n");
for(i=0;i<8;i++)
scanf("%f",&x[i]);
a[0]=x[0]+x[4]; a[1]=x[0]-x[4];
a[2]=x[2]+x[6];a[3]=x[2]-x[6];
a[4]=x[1]+x[5];a[5]=x[1]-x[5];
a[6]=x[3]+x[7];a[7]=x[3]-x[7];
br[0]=a[0]+a[2];bi[0]=0;
br[1]=a[1];bi[1]=-a[3];
br[2]=a[0]-a[2];bi[2]=0;
br[3]=a[1];bi[3]=a[3];
br[4]=a[4]+a[6];bi[4]=0;
br[5]=a[5];bi[5]=-a[7];
br[6]=a[4]-a[6];bi[6]=0;br[7]=a[5];
bi[7]=a[7];
p=((br[5]*0.707)-(bi[5]*(-0.707)));
q=((br[5]*(-0.707))+(bi[5]*0.707));
r=((br[7]*(-0.707))-(bi[7]*(-0.707)));
s=((br[7]*(-0.707))+(bi[7]*(-0.707)));
Xr[0]=br[0]+br[4];Xi[0]=0;
Xr[1]=p+br[1];Xi[1]=q+bi[1];
Xr[2]=br[2];Xi[2]=-br[6];
Xr[3]=r+br[3];Xi[3]=s+bi[3];
Xr[4]=br[0]-br[4];Xi[4]=0;
Xr[5]=br[1]-p;Xi[5]=bi[1]-q;
Xr[6]=br[2];Xi[6]=br[6];
Xr[7]=br[3]-r;Xi[7]=bi[3]-s;
printf("8 point DITFFT");
for(i=0;i<8;i++)
printf("\n%.3f+(%.3fi)",Xr[i],Xi[i]);
printf("\nMagnitude and phase");
for(i=0;i<8;i++)
{Mag[i]=sqrt((Xr[i]*Xr[i])+(Xi[i]*Xi[i]));
pha[i]=(atan(Xi[i]/Xr[i])*180)/pi;
if(Xi[i]==0) pha[i]=0;
if(Xr[i]>=0 && Xi[i]>=0) pha[i]=pha[i];
else if(Xr[i]>=0 && Xi[i]<=0) pha[i]=360+pha[i];
else pha[i]=180+pha[i];
printf("\n%.3f\t%.3f",Mag[i],pha[i]);}}
5)IFFT
#include <stdio.h>
float
main()
{
float XR[8],XI[8],ar[8],ai[8], br[8],bi[8], xr[8], xi[8], xrf[8], xif[8];
float p,q,r,s,l;
int i;
printf("8: XR(M); XI(m)");
for (i=0;i<8;i++)
{ printf(" XR[%d]:",i);
scanf("%f",&XR[i]);
printf(" XI[%d]:",i);
scanf("%f",&XI[i]);
}
ar[0]=XR[0]+XR[4]; ai[0]=XI[0]+XI[4];
ar[1]=XR[1]+XR[5]; ai[1]=XI[1]+XI[5];
ar[2]=XR[2]+XR[6]; ai[2]=XI[2]+XI[6];
ar[3]=XR[3]+XR[7]; ai[3]=XI[3]+XI[7];
ar[4]=XR[0]-XR[4]; ai[4]=XI[0]-XI[4];
ar[5]=XR[1]-XR[5];
ai[5]=XI[1]-XI[5];
p= ar[5]-ai[5];
q= ar[5]+ai[5];
ar[5]= (p)*0.707;
ai[5]= (q)*0.707;
ar[6]=XR[2]-XR[6];
ai[6]=XI[2]-XI[6];
l=ar[6];
ar[6]= -ai[6];
ai[6]= l;
ar[7]=XR[3]-XR[7];
ai[7]=XI[3]-XI[7];
r= ar[7]+ai[7];
s=ar[7]-ai[7];
ar[7]= -r*0.707;
ai[7]= s*0.707;
printf("ii");
for (i=0;i<8;i++)
{
printf("\n %.3f+(%.3f i)",ar[i],ai[i]);
}
printf("b");
br[0]= ar[0]+ar[2]; bi[0]= ai[0]+ai[2];
br[1]= ar[1]+ar[3]; bi[1]= ai[1]+ai[3];
br[2]= ar[0]-ar[2]; bi[2]= ai[0]-ai[2];
br[3]= ar[1]-ar[3];
bi[3]= ai[1]-ai[3];
l=br[3];
br[3]= -bi[3];
bi[3]= l;
br[4]= ar[4]+ar[6]; bi[4]= ai[4]+ai[6];
br[5]= ar[5]+ar[7]; bi[5]= ai[5]+ai[7];
br[6]= ar[4]-ar[6]; bi[6]= ai[4]-ai[6];
br[7]= ar[5]-ar[7];
bi[7]= ai[5]-ai[7];
l=br[7];
br[7]= -bi[7];
bi[7]= l;
for (i=0;i<8;i++)
{
printf("\n %.3f+(%.3f i)",br[i],bi[i]);
}
printf("\n x");
xr[0]= br[0]+br[1]; xi[0]= bi[0]+bi[1];
xr[1]= br[0]-br[1]; xi[1]= bi[0]-bi[1];
xr[2]= br[2]+br[3]; xi[2]= bi[2]+bi[3];
xr[3]= br[2]-br[3]; xi[3]= bi[2]-bi[3];
xr[4]= br[4]+br[5]; xi[4]= bi[4]+bi[5];
xr[5]= br[4]-br[5]; xi[5]= bi[4]-bi[5];
xr[6]= br[6]+br[7]; xi[6]= bi[6]+bi[7];
xr[7]= br[6]-br[7]; xi[7]= bi[6]-bi[7];
for (i=0;i<8;i++)
{xr[i]= xr[i]/8;
xi[i]= xi[i]/8;
printf("\n %.3f+(%.3f)",xr[i],xi[i]);
}

xrf[0]= xr[0];
xrf[7]=xr[7];
xrf[2]=xr[2];
xrf[5]=xr[5];
xrf[4]=xr[1];
xrf[1]=xr[4];
xrf[3]=xr[6];
xrf[6]=xr[3];
xif[0]= xi[0];
xif[7]=xi[7];
xif[2]=xi[2];
xif[5]=xi[5];
xif[4]=xi[1];
xif[1]=xi[4];
xif[3]=xi[6];
xif[6]=xi[3];
for (i=0;i<8;i++)
{
printf("\n %.3f+(%.3f)",xrf[i],xif[i]);
}
Output:
Enter input sequence
28
0
-9
4
-4
-4
1.657
-4
4
0
1.657
4
-4
4
-9.657
-4
8point IFFT
1.082+(-0.000)
2.058+(0.059)
3.000+(0.084)
3.942+(0.056)
4.918+(-0.000)
5.942+(-0.059)
7.000+(-0.084)
0.058+(-0.056)
7.sum sine

fs=1000
f1=50
f2=100
n=0:1:199
s1=sin(2*pi*f1*n/fs)
sk1=abs(fft(s1))
phase1=atan(imag(fft(s1))./real(fft(s1)))
s2=sin(2*pi*f2*n/fs)
sk2=abs(fft(s2))
phase2=atan(imag(fft(s2))./real(fft(s2)))
s=s1+s2
sk=abs(fft(s))
phase3=atan(imag(fft(s))./real(fft(s)))
subplot(3,3,1)
stem(n(1:50)/fs,s1(1:50))
xlabel('radians')
ylabel('amplitude')
title('First sin signal')
subplot(3,3,2)
stem(n(1:50)/fs,s2(1:50))
xlabel('radians')
ylabel('amplitude')
title('Second sin signal')
subplot(3,3,3)
stem(n(1:50)/fs,s(1:50))
xlabel('radians')
ylabel('amplitude')
title('sum signal')
subplot(3,3,4)
stem(n(1:30)*fs/200,sk1(1:30))
xlabel('frequency')
ylabel('amplitude')
title(' Magnitude response of first signal')
subplot(3,3,5)
stem(n(1:30)*fs/200,sk2(1:30))
xlabel('frequency')
ylabel('amplitude')
title(' Magnitude response of second signal')
subplot(3,3,6)
stem(n(1:30)*fs/200,sk(1:30))
xlabel('frequency')
ylabel('amplitude')
title(' Magnitude response of sum signal')
subplot(3,3,7)
stem(n(1:30)*fs/200,phase1(1:30))
xlabel('frequency')
ylabel('phase')
title(' phase response of first signal')
subplot(3,3,8)
stem(n(1:30)*fs/200,phase2(1:30))
xlabel('frequency')
ylabel('phase')
title(' phase response of second signal')
subplot(3,3,9)
stem(n(1:30)*fs/200,phase3(1:30))
xlabel('frequency'); ylabel('phase'); title(' phase response of sum signal')
8a) butterworrh 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')

9a) chebyshev low pass


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')
10)windowing tech
clc;
clear all;
close all;
n=input('Enter order of the filter: ');
fp=input('Enter the passband frequency: ');
fs=input('Enter the passband frequency: ');
f=input('Enter the passband frequency: ');
wp=(2*fp)/f;
ws=(2*fs)/f;
%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,2,1);
plot(o/pi,m);
title('Magnitude response of Rectangular Window LPF ');
xlabel('normalised frequency');
ylabel('gain in db');
grid on;
%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,2,2);
plot(o/pi,m);
title('Magnitude response of Rectangular Window HPF');
xlabel('normalised frequency');
ylabel('gain in db');
grid on;
%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,2,3);
plot(o/pi,m);
title('Magnitude response of Hamming Window LPF ');
xlabel('normalised frequency');
ylabel('gain in db');
grid on;
%high pass filter
window=hamming(n+1);
b=fir1(n,wp,'high',window);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(3,2,4);
plot(o/pi,m);
title('Magnitude response of Hamming Window HPF');
xlabel('normalised frequency');
ylabel('gain in db');
grid on;
%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,2,5);
plot(o/pi,m);
title('Magnitude response of Kaiser Window LPF ');
xlabel('normalised frequency');
ylabel('gain in db');
grid on;
%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,2,6);
plot(o/pi,m);
title('Magnitude response of Kaiser Window HPF');
xlabel('normalised frequency');---(next= ylabel('gain in db');-- grid on;)
11)DECIMATION and INTERPOLATION Source Code
clc;
clear all;
close all;
%continuous sinusoidal signal
a=input('Enter the amplitude:');
f=input('Enter the Timeperiod:');
t=-10:1:20;
x=a*sin(2*pi*f*t);
subplot(2,3,1);
plot(t,x);
xlabel('time');
ylabel('Amplitude');
title('Sinusoidal signal');
%decimating the signal
d=input('Enter the value by which the signal is to
be decimated:');
y1=decimate(x,d);
subplot(2,3,2);
stem(y1);
xlabel('time');
ylabel('Amplitude');
title('Decimated signal');
%interpolating the signal
i=input('Enter the value by which the signal is to
be interpolated:');
y2=interp(x,i);
subplot(2,3,3);
stem(y2);
xlabel('time');
ylabel('Amplitude');
title('Interpolated signal');
%resampling the signal
y3=resample(x,3,2);
subplot(2,3,4);
stem(y3);
xlabel('time');
ylabel('Amplitude');
title('Resampled signal');
%downsampling the signal
y4=downsample(x,2);
subplot(2,3,5);
stem(y4);
xlabel('time');
ylabel('Amplitude');
title('Downsampled signal');
%upsampling the signal
y5=upsample(x,3);
subplot(2,3,6);
stem(y5);
xlabel('time');
ylabel('Amplitude');
title('Upsampled signal');
12)Dft

clc;
clear all;
close all;
N=input('Enter the value of N');
x=input('Enter the input sequence X(n):');
t=0:N-1;
subplot(2,1,1);
stem(t,x);
xlabel('TIME');
ylabel('AMPLITUDE');
title('INPUT SIGNAL');
grid on;
y=fft(x,N);
subplot(2,1,2);
stem(t,y);
xlabel('TIME');
ylabel('AMPLITUDE');
title('OUTPUT SIGNAL');
grid on;

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