Download complete C Programes

A hand book of programs for beginners & students.



MATLAB: circular convolution using DFT

Q=Find the circular convolution of the sequences S1(n) = [1, 2,1, 2] and S2(n) = [3, 2, 1, 4]; Verify the result using DFT method.

x1=[1, 2,1, 2];
x2=[3, 2, 1, 4];
c=fcconv(x1,x2);
subplot(211);
stem(c);grid;
title('Direct');
X1=fft(x1);
X2=fft(x2);
X=X1.*X2;
x=ifft(X);
subplot(212);
stem(x);grid;
title('using DFT');

Function




function [c]=fcconv(x1,x2)
if length(x1)>length(x2)
    x2=[x2, zeros(1,length(x1)-length(x2))];
else
    x1=[x1, zeros(1,length(x2)-length(x1))];
end
l=length(x1);
n=1:l;
y(n)=0;
for k=1:l
p=(mod(n-k,l))+1;
y(n)=y(n)+ x1(k)*x2(p);
end
c=y;

MATLAB : Convolution Using DFT

Q:1. Find the linear convolution of the sequences S1(n) = {1, -2,-2, 1} and S2(n) = {-1, 1, 1, -1}; Verify the result using convolution property. 

clc;
x1=input('x1 : ');
x2=input('x2 : ');
x=conv(x1,x2);
subplot(211);
stem(x);
title('Answer using direct convelution');
n=length(x1)+length(x2)-1;
X1=fft(x1,n);
X2=fft(x2,n);
X=X1.*X2;
xc=ifft(X,n);
subplot(212);
stem(xc);
title('Answer using DFT method');

MATLAB : Sampling

t=0:.0001:.05;
n=1:1:200;
x=5*sin(2*pi*fk*t);
fs=input('Enter sampling freq : ');
ts=1/fs;
subplot(3,1,1);
plot(t,x);
t=0:ts:.05;
y=5*sin(2*pi*100*t);
subplot(3,1,2);
stem(y);
subplot(3,1,3);
plot(t,y);

MATLAB: circular convolution

clc;
close all;
x1=input('Enter x1 : ');
x2=input('Enter x2 : ');
c=fcconv(x1,x2);
c
stem(c);
title('Circular convolution');
xlabel('--------> n');
ylabel('Amp');

function
function [c]=fcconv(x1,x2)
if length(x1)>length(x2)
    x2=[x2, zeros(1,length(x1)-length(x2))];
else
    x1=[x1, zeros(1,length(x2)-length(x1))];
end
l=length(x1);
n=1:l;
y(n)=0;
for k=1:l
p=(mod(n-k,l))+1;
y(n)=y(n)+ x1(k)*x2(p);
end
c=y;

MATLAB: Linear convolution

clc;
close all;
x=input('Enter input sequence : ');
sx=input('Enter starting index : ');
h=input('Enter transfer sequence : ');
sh=input('Enter starting index : ');
c=conv(x,h);
n=sx+sh:sx+sh-1+length(c);
stem(n,c);