% 1 Sum of Sinusoidal Signals
% 1 Sum of Sinusoidal Signals
1:10 f=input(enter the frequency) x=sin(2*pi*f*t) figure subplot(3,1,1) plot(t,x) xlabel(time period) ylabel(amplitude) title(sine wave) y=cos(2*pi*f*t) subplot(3,1,2) plot(t,y) xlabel(time period) ylabel(amplitude) title(cosine wave) z=x+y subplot(3,1,3) plot(t,z) xlabel(time period) ylabel(amplitude) title(sum of signals) % 2 LINEAR CONVOLUTION AIM: To verify Linear Convolution of two sequences using MATLAB. A) Using inbuilt function. B) Without using inbuilt function.. EQUIPMENTS: Constructor - Simulator Software - MATLAB 7.5 PROCEDURE: Using inbuilt function: 1) Generate the first input sequence x. 2) Generate the second input sequence y. 3) Use the inbuilt function conv(). Z=conv(x,y) 4) Plot the output sequences. 5) Make use of subplot() to plot the inputs and output sequences in a single window. Without using inbuilt function: 6) Find out the length of the first sequence and store it in variable n1. 7) Similarly find out the length of the second sequence and store it in n2. 8) Find the length of the linearly convolved sequence I
n3=n1+n2-1 9) Make the both the sequences x & y of length n3 by padding relevant number of zeros. 10) Perform the circular convolution of the sequences x and y and store it in z. 11) Linear convolution of x and y is given by the following equation: Z(n)= x( i ) . y( j ) where n=0 to (n3-1), j= (n-i) n=0 But in MATLAB x(0) does not exist. So i takes vales from 1 to n3. Similarly for n. Therfore, the above equation becomes; Z(n)= x( i ) . y( j ) n=1 12)Display and plot the convolved sequences. 13) Compare theoretical and practical values. INPUTS: x=[1 2 3 4] y=[1 0 1 1] PROGRAM: clc clear all close all x=input(enter the first sequence) y=input(enter the second sequence) % using inbuilt function w=conv(x,y) % without using inbuilt function L=length(x) m=length(y) k=L+m-1 x=[x,zeros(1,m-1)] y=[y,zeros(1,L-1)] for n=1:k z(n)=0 for s=1:k a=n-s+1 if(a<=0) a=a+k end z(n)=z(n)+(x(s)*y(a)) end end % plotting the waveforms II where n=1 to n3, j= (n-i+1)
figure subplot(2,2,1) stem(x) title(first input sequence) xlabel(n--------->) ylabel(amplitude) subplot(2,2,2) stem(y) title(second input sequence) xlabel(n--------->) ylabel(amplitude) subplot(2,2,3) stem(w) title(output sequence (inbuilt)) xlabel(n--------->) ylabel(amplitude) subplot(2,2,4) stem(z) title(output sequence (without using inbuilt)) xlabel(n--------->) ylabel(amplitude) % 3 CIRCULAR CONVOLUTION AIM To verify Circular Convolution. EQUIPMENTS: Constructor - Simulator Software - MATLAB 7.5 PROCEDURE: 1) Generate the first input sequence x . 2) Generate the second input sequence y. 3) Find out the length of the first sequence and store it in variable n1. 4) Similarly find out the length of the second sequence and store it in n2. 5) Find out the maximum length sequence among x and y. Let the maximum length n3. [use max( ) function] 6) Make the length of the smaller sequence equal to the larger sequence by padding zeros. 7) Perform the circular convolution of the sequences x and y and store it in z. III
8) Circular convolution of x and y is given by the following equation: Z(n)= x( i ) . y( j ) where n=0 to (n3-1), j= (n-i)n3. n=0 But in MATLAB x(0) does not exist. So i takes vales from 1 to n3. Similarly for n. Therfore the above equation becomes; Z(n)= x( i ) . y( j ) n=1 where n=1 to n3, j= (n-i+1)n3
9) Display and plot the convolved sequences. 10) Make use of subplot() to plot the inputs and output sequences in a single window. 11) Compare theoretical and practical values. INPUTS: x=[1 2 3 4] y=[1 0 1 1] PROGRAM: clc clear all close all x=input(enter the first sequence) y=input(enter the second sequence) L=length(x) m=length(y) k=max(L,m) if(k==m) x=[x,zeros(1,m-L)] end if(k==L) y=[y,zeros(1,L-m)] end for n=1:k z(n)=0 for s=1:k a=n-s+1 if(a<=0) a=a+k end z(n)=z(n)+(x(s)*y(a)) end end % plotting the waveforms figure subplot(3,1,1) IV
stem(x) title(first input sequence) xlabel(n--------->) ylabel(amplitude) subplot(3,1,2) stem(y) title(second input sequence) xlabel(n--------->) ylabel(amplitude) subplot(3,1,3) stem(z) title(output sequence) xlabel(n--------->) ylabel(amplitude) % 4 FFT AIM: To write a MATLAB program to compute FFT for a given sequence and plot the graph. clc clear all close all x=input(enter the sequence) n=input(enter the length of fft) y=fft(x,n) figure stem(y) title(fast fourier transform) xlabel(real axis) ylabel(imaginary axis)
% 5 AUTO-CORRELATION & PSD AIM: To find the power spectral density of a given input sequence and plot the graph.. EQUIPMENTS: Constructor - Simulator Software - MATLAB 7.5 PROCEDURE: 1) Enter an input sequence x of length N 2) Plot the above sequence. 3) Give relevant names to x- and y- axes and appropriate title for the plot. 4) Compute autocorrelation y of input sequence x by using xcorr() function. V
5) Plot the resultant autocorrelation function y. [repeat steps 2 and 3] 6) Calculate power spectral density of the input sequence by computing the Fourier transform of the ACF y using fft( ) function. 7) Plot the PSD function of x. 8) Compare the result with theoretical values where ACF can be calculated using the formula:
k= (N-1)
y(n)= x( k ) . y( k-n )
k= - (N-1)
PROGRAM: clc clear all close all x = input(enter the sequence) y = xcorr(x, x) or y = xcorr(x) n=1 p = fft(y, n) % plotting the waveforms figure subplot(3,1,1) stem(x) title( input sequence) xlabel(n--------->) ylabel(amplitude) subplot(3,1,2) stem(y) title(auto correlated sequence) xlabel(n--------->) ylabel(amplitude) subplot(3,1,3) stem(p) title(power density spectrum) xlabel(n--------->) ylabel(amplitude)
VI
% 1 LINEAR CONVOLUTION USING CC STUDIO #include<stdio.h> main() { int, i, j, k, n, m ,L, s, p; int a[20], b[20],c[30]; printf(enter length of first sequence); scanf(%d, &m); printf(enter first sequence); for(i=0; i<=m-1; i++) scanf(%d, &a[i]); printf(enter length of second sequence); scanf(%d, &n); printf(enter second sequence); for(j=0; j<=n-1; j++) scanf(%d, &b[j]); L=m+n-1; for(i=m; i<L; i++) a[i]=0; for(j=n; j<L; j++) b[j]=0; for(k=0;k<=L-1;k++) { c[k]=0; for(s=0;s<=L-1;s++) { p=k-s; if(p<0) p=p+L; c[k]=c[k]+(a[s]*b[p]); } } printf(resultant seq is\n); for(k=0; k<=L-1; k++) { printf(%d \n, c[k]); } VII
#include<stdio.h> main() { int, i, j, k, n, m, q, s, p; int a[20], b[20],c[30]; printf(enter length of first sequence); scanf(%d, &m); printf(enter first sequence); for(i=0; i<=m-1; i++) scanf(%d, &a[i]); printf(enter length of second sequence); scanf(%d, &n); printf(enter second sequence); for(j=0; j<=n-1; j++) scanf(%d, &b[j]); if(m>n) { k=m; for(j=n; j<k; j++) b[j]=0; } else if (n>m) { k=n; for(i=m; i<k; i++) a[i]=0; } else { k=m; } for(p=0; p<=k-1; p++) { c[p]=0; for(s=0; s<=k-1; s++) { q=p-s; if(q<0) { q=q+k; VIII
} c[p]=c[p]+(a[s]*b[q]); } } printf(resultant seq is\n); for(p=0; p<=k-1; p++) { printf(%d \n, c[p]); } }
IX
% 3 COMPUTATION OF DFT #include<stdio.h> #include<math.h> main( ) { float x[8],re[8], imgs[8], q, w, e, resum, imsum, coss,cins; int leng, i, j, k; printf(enter num of elements); scanf(%d, &leng); for(i=0; i<leng; i++) { printf(enter %d element, i); scanf(%f, &x[i]); } for(i=0; i<leng; i++) { resum=0; imsum=0; for(k=0; k<leng; k++) { q=2.0*k*3.14*(i/leng); coss=cos(q); sins=sin(q); resum=resum+x[k]*coss; imsum=imsum+x[k]*sins; } re[i]=resum; imgs[i]=imsum; } for(j=0; j<leng; j++) { printf(%f+j%f, re[j], imgs[j]); } scanf(%d, j); }