Sunteți pe pagina 1din 14

DSP PRACTICAL

Name: Kiran Kannan


MIS NO: 111407065 (TY E&TC)

INDEX:
1. Signal generation
2. Sampling
3. Circular Convolution
4. Linear Convolution
5. DFT
6. FIR Filter Design
7. IIR Filter Design

Generation of Cosine Wave


Aim:
To generate cosine wave in Turbo C/C++

Algorithm:
Environment used for C coding was Turbo C/C++. For the graph the graphics library WINBGI was
added. The amplitude, sine wave frequency and sampling frequency were input by user. Line
function was used to plot amplitude and time axis. Sine function value was computed using direct
formula from library math.h.

Program:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<math.h>
#include<dos.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
double x, y;
int i=0,fq,amp;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "C:\\TC\\bgi");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
x=0;
printf("Enter the required frequency in multiples of pi: ");
scanf("%d",&fq);
printf("\nEnter the required amplitude : ");
scanf("%d", &amp);
line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
while(x<getmaxx())
{
setcolor(15);

y=amp*cos((3.14*fq*x)/180);
y=y+getmaxy()/2;
putpixel(x,y,15);
delay(0.5);
x=x+0.01;
i++;
}
getch();
closegraph();
return 0;
}

Output:

Generation of Sampled Sine Wave in C


Aim:
To generate sine wave in Turbo C/C++ and sampling it with sampling frequency greater than Nyquist
theorem.

Algorithm:
Environment used for C coding was Turbo C/C++. For the graph the graphics library WINBGI was
added. The amplitude, sine wave frequency and sampling frequency were input by user. Line
function was used to plot amplitude and time axis. Sine function value was computed using direct
formula from library math.h. In sampling of the signal the value of the continuous wave at discrete
instances were measured and plotted.

Program:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<math.h>
#include<dos.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
double x, y,n;
int i=0,fq,amp,ts;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "C:\\TC\\bgi");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
x=0;

printf("Enter the required frequency in multiples of pi: ");


scanf("%d",&fq);
printf("Enter the required sampling time: ");
scanf("%d",&ts);
printf("\nEnter the required amplitude : ");
scanf("%d", &amp);
n=(2*fq)/ts;
line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
while(x<getmaxx())
{
setcolor(15);
y=amp*sin((3.14*n*x)/180);
y=y+getmaxy()/2;
putpixel(x,y,15);
line(x,getmaxy()/2,x,y-12);
delay(5);
x=x+5;
i++;
}
struct time t;
gettime(&t);
printf("The current time is: %2d:%02d:%02d.%02d\n",
t.ti_hour, t.ti_min, t.ti_sec, t.ti_hund);
getch();
closegraph();
return 0;
}

Output:

Circular Convolution in C
Aim:
To circularly convolve two sequences taken as input from the user, in Turbo C

Algorithm: The matrix method for circular convolution was used to generate a matrix using
circularly shifted matrix 1, to this NxN matrix we multiplied matrix 2 to get desired circular
convolution result.
Program:
#include<iostream.h>
#include<conio.h>
void main() {
int n=0, i, j, k, c, x1[100]={0}, x2[100]={0}, y[100]={0},temp[100], h[100][100]={0};
clrscr();
cout<<"\n\n\t\tEnter number of elements in the sequences (N1=N2) : ";
cin>>n;
cout<<"\n\n\t\tEnter first sequence : ";
for(i=0; i<n ; i++) cin>>x1[i];
cout<<"\n\n\t\tEnter second sequence : ";
for(i=0; i<n ; i++)
cin>>x2[i];
for(i=0; i<n; i++)
{
temp[i]=i;
}
for(j=0; j<n; j++) {
for(i=0; i<n ; i++) {
h[i][j]=x2[temp[i]];
}
//circular rotation of array index
c=temp[n-1];
for(k=n-1; k>0; k--)
temp[k]=temp[k-1];
temp[0]=c; }
//h[n][n]*x[n][1]
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
y[i]=y[i]+h[i][j]*x1[j];
}
}
cout<<"\n\n\t\tCircular Convolution result : ";
for(k=0; k<n; k++)

cout<<y[k]<<", ";
getch();
}

Output:

Linear Convolution in C
Aim: To convolve two sequences taken as input from the user, in Turbo C/C++
Algorithm: Running two nested for loops and using normal formula method convolution of the
input sequences was computed.

Program:
#include<iostream.h>
#include<conio.h>
#include<math.h >
void main() {
clrscr();
float x[100]={0}, h[100]={0}, y[200]={0}, num; int Nx=0, Nh=0, temp, n=0, k;
//lengths of sequences 'x' nd 'h'
cout<<"\n\n\t\tEnter x(n) (Enter 999 to end) : ";
do {
cin>>num;
if(num != (float)999) x[n++] = num; }while(num != (float)999);
Nx=n; //length of sequence 'x'
cout<<"\n\n\t\tEnter h(n) (Enter 999 to end) : ";
n=0;
do {
cin>>num;
if(num != (float)999) h[n++] = num; }while(num != (float)999);
Nh=n; //length of sequence 'h'
for(n=0; n < Nx+Nh-1; n++) {
for(k=0; k<Nx+Nh-1; k++) {
if(n-k>=0)
y[n] += x[k]*h[n-k];
else continue; }
}
cout<<"\n\n\t\tConvolution of x(n) & h(n) is:\n";
for(n=0; n < Nx+Nh-1; n++)
cout<<"y("<<n<<") = "<<y[n]<<"\n";
getch();
}

Output:

Discrete Fourier Transform (DFT) in C


Aim: To evaluate DFT of various sequences using Turbo C
Algorithm: Two functions were created for returning real and imaginary parts of twiddle factors.
Running two nested for loops and using normal formula method DFT of the input sequence was
computed.

Program: #include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<math.h>
#include<graphics.h>
#define PI 3.1415
void main()
{ int gdriver=DETECT;
int gmode,err;
float x[20]={0};
int n,r,k;
float p,re,img;
clrscr();
initgraph(&gdriver,&gmode,"C:\\TC\\bgi");
err=graphresult();
if(err!=0)
{ printf("Graphics error:%s\n",grapherrormsg(err)); }
struct date d;
getdate(&d);
printf("The date is: %d/%d/%d\n",d.da_day,d.da_mon,d.da_year);
struct time t;
gettime(&t);
printf("The time is: %d:%d:%d\n",t.ti_hour,t.ti_min,t.ti_sec);
printf("Enter the no. of numbers:");
scanf("%d",&r);
printf("\n");
float y1[20]={0};
float y2[20]={0};
printf("Enter the elements:");
for(n=0;n<r;n++)
scanf("%f",&x[n]);
printf("\n");
for(k=0;k<r;k++)
{ for(n=0;n<r;n++)
{ p=-2*PI*k*n/r;

re=cos(p);
img=sin(p);
y1[k]=y1[k] + (x[n]*re);
y2[k]=y2[k] + (x[n]*img); } }
printf("The DFT of entered sequence is:");
for(n=0;n<r;n++)
printf("%.3f + (%.3fj) \t",y1[n],y2[n]);
getch();
}

Output:

FIR Filters
Aim:
To design FIR Filters using the windowing function

Algorithm:
First the desired frequency response is generated through Hd. The desired response Hd() is
displayed and hd(n) is evaluated by taking IDTFT of Hd(). Thus, the desired characteristics of the FIR
filter have been plotted. w(n), window function in time domain is created using input N from the
user which is the length of the windowing function. Now, W() is found by taking DTFT of w(n) and
is plotted. This is followed by term by term multiplication of hd(n) and w(n) giving us h(n), the actual
or post windowing characteristics of our FIR filter. Again, DTFT of h(n) gives us H(), both of which
are plotted. For plotting we use subplot function to display all graphs in the same window.

Program:
n= input('Enter the number of samples: ')
X = linspace(0,pi/2,n);
Y = X.^0; ylabel('Amplitude')
xlabel('Time')
subplot(2,2,1)
stem(X,Y)
title('Windowing Function')
for s = 0.0: (pi/2)/n: pi/2
end
P = linspace(-7*pi,7*pi,28*n);
Q =sinc(P);
ylabel('Amplitude');
xlabel('Time');
subplot(2,2,3);
stem(P,Q);
title('H(n)')
T= linspace(0,pi/2,n);
S= sinc(T);
ylabel('Amplitude')
xlabel('Time')
subplot(2,2,2)
stem(T,S);
title('Filter Response')
A = linspace(0,pi,28*n);
B=sinc(A);
ylabel('Amplitude')
xlabel('Time')
subplot(2,2,4);

stem(A,B);

Output:

IIR Filters
Aim:
To design a Butterworth low pass IIR Filter

Algorithm:
Find the prewarped frequencies using given specifications. Find the order of the filter buttord()
function. Denormalize the transfer function of oder found using butter() function. Find the digital
transfer function H(z) by Impulse Invariant technique using command impinvar().

Program:
clear all
alphaap=4; %passband attenuation in dB
alphas=30; %stopband attenuation in dB
fp=400;
% passband frequency in Hz
fs=800;
%stopband frequency in Hz
F=2000;
%Sampling frequency in Hz
omp=2*fp/F; oms=2*fs/F
%To find the cutoff frequency and the order of the filter
[n,wn]=buttord(omp,oms,alphaap,alphas); %system function of the filters
[b,a]=butter(n,wn);
w=0:0.01:pi;
[h,om]=freqz(b,a,w,whole);
m=abs(h);
an=angle(h);
subplot(2,1,1);
plot(om/pi,20*log(m));grid;

ylabel(gain in dB);
xlabel(normalized frequency);
subplot(2,1,2):plot(om/pi,an);grid;
ylabel(phase in radian);
xlabel(Normalized frequency);

Output:

S-ar putea să vă placă și