80% found this document useful (5 votes)
16K views

Linear Convolution Program in C Language Using CCStudio

The document describes programs to implement linear and circular convolution in C and MATLAB. The C program performs linear convolution by padding the smaller input sequence with zeros, multiplying corresponding elements, and summing the results. The MATLAB program performs both linear and circular convolution, padding sequences if needed and using a for-loop to multiply and sum elements. It displays the input sequences and output convolution.

Uploaded by

sk_ebook
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
80% found this document useful (5 votes)
16K views

Linear Convolution Program in C Language Using CCStudio

The document describes programs to implement linear and circular convolution in C and MATLAB. The C program performs linear convolution by padding the smaller input sequence with zeros, multiplying corresponding elements, and summing the results. The MATLAB program performs both linear and circular convolution, padding sequences if needed and using a for-loop to multiply and sum elements. It displays the input sequences and output convolution.

Uploaded by

sk_ebook
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Linear convolution program in c language using MATLAB program for linear convolution

CCStudio clc;
#include<stdio.h> clear all;
int x[15],h[15],y[15]; close all;
main() disp('linear convolution program');
{ x=input('enter i/p x(n):');
int i,j,m,n; m=length(x);
printf("\n enter value for m"); h=input('enter i/p h(n):');
scanf("%d",&m); n=length(h);
printf("\n enter value for n"); x=[x,zeros(1,n)];
scanf("%d",&n); subplot(2,2,1), stem(x);
printf("Enter values for i/p x(n):\n"); title('i/p sequencce x(n)is:');
for(i=0;i<m;i++) xlabel('---->n');
scanf("%d",&x[i]); ylabel('---->x(n)');grid;
printf("Enter Values for i/p h(n) \n"); h=[h,zeros(1,m)];
for(i=0;i<n; i++) subplot(2,2,2), stem(h);
scanf("%d",&h[i]); title('i/p sequencce h(n)is:');
// padding of zeors xlabel('---->n');
for(i=m;i<=m+n-1;i++) ylabel('---->h(n)');grid;
x[i]=0; disp('convolution of x(n) & h(n) is y(n):');
for(i=n;i<=m+n-1;i++) y=zeros(1,m+n-1);
h[i]=0; for i=1:m+n-1
/* convolution operation */ y(i)=0;
for(i=0;i<m+n-1;i++) for j=1:m+n-1
{y[i]=0; if(j<i+1)
for(j=0;j<=i;j++) y(i)=y(i)+x(j)*h(i-j+1);
{y[i]=y[i]+(x[j]*h[i-j]); end
}}//displaying the o/p end
for(i=0;i<m+n-1;i++) end
printf("\n The Value of output y[%d]=%d",i,y[i]); ysubplot(2,2,[3,4]),stem(y);
} title('convolution of x(n) & h(n) is :');
Result: xlabel('---->n');
enter value for m 5 ylabel('---->y(n)');
enter value for n 5
Enter values for i/p program to implement circular convolution
12345Enter Values for h
12345The Value of output y[0]=1 #include<stdio.h>
int m,n,x[30],h[30],y[30],i,j, k,x2[30],a[30];
The Value of output y[1]=4 void main()
The Value of output y[2]=10 {
The Value of output y[3]=20 printf(" enter the length of the first sequence\n");
The Value of output y[4]=35 scanf("%d",&m);
The Value of output y[5]=44 printf(" enter the length of the second sequence\n");
The Value of output y[6]=46 scanf("%d",&n);
The Value of output y[7]=40 printf(" enter the first sequence\n");
The Value of output y[8]=25 for(i=0;i<m;i++)
scanf("%d",&x[i]);
printf(" enter the second sequence\n");
for(j=0;j<n;j++) m=n;
scanf("%d",&h[j]);
if(m-n!=0) }
/*If length of both sequences are not equal*/ y[0]=0;
{ a[0]=h[0];
if(m>n) for(j=1;j<n;j++)
/* Pad the smaller sequence with zero* /*folding h(n) to h(-n)*/
{ a[j]=h[n-j];
for(i=n;i<m;i++) /*Circular convolution*/
h[i]=0; for(i=0;i<n;i++)
n=m;
}for(i=m;i<n;i++) y[0]+=x[i]*a[i]; for(k=1;k<n;k++) {
x[i]=0; y[k]=0;
m=n; /*circular shift*/
for(j=1;j<n;j++)
} x2[j]=a[j-1];
y[0]=0; x2[0]=a[n-1];
a[0]=h[0]; for(i=0;i<n;i++)
for(j=1;j<n;j++)
/*folding h(n) to h(-n)*/ {
a[j]=h[n-j]; a[i]=x2[i];
/*Circular convolution*/ y[k]+=x[i]*x2[i];
for(i=0;i<n;i++) }
}
y[0]+=x[i]*a[i]; for(k=1;k<n;k++) {
y[k]=0; /*displaying the result*/
/*circular shift*/ printf(" the circular convolution is\n");
for(j=1;j<n;j++) for(i=0;i<n;i++)
x2[j]=a[j-1]; printf("%d \t",y[i]);
x2[0]=a[n-1]; }
for(i=0;i<n;i++) OUTPUT:-
Enter the first sequence
{
a[i]=x2[i]; 567
y[k]+=x[i]*x2[i]; Enter the second sequence
} 7854OUTPUT ;-
} the circular convolution is
94
/*displaying the result*/ 110
printf(" the circular convolution is\n"); 122
for(i=0;i<n;i++) 106
printf("%d \t",y[i]);
} circular convolution program
for(i=n;i<m;i++)
h[i]=0; clc;
n=m; clear all;
}for(i=m;i<n;i++) close all;
x[i]=0; disp('circular convolution program');
end
x=input('enter i/p x(n):'); end
m=length(x); end
h=input('enter i/p sequence h(n)'); ysubplot(2,2,[3,4]),stem(y);
n=length(h);
title('convolution of x(n) & h(n) is:');
subplot(2,2,1), stem(x); xlabel('---->n');
title('i/p sequencce x(n)is:'); ylabel('---->y(n)');grid;
xlabel('---->n');
ylabel('---->x(n)');grid;

subplot(2,2,2), stem(h);
title('i/p sequencce h(n)is:');
xlabel('---->n');
ylabel('---->h(n)');grid;
disp('circular convolution of x(n) & h(n) is y(n):');
if(m-n~=0)

if(m>n)
h=[h,zeros(1,m-n)];
n=m;
endx=[x,zeros(1,n-m)];
m=n;
end

y=zeros(1,n);
y(1)=0;
a(1)=h(1);
for j=2:n
a(j)=h(n-j+2);
end
%ciruclar conv

for i=1:n
y(1)=y(1)+x(i)*a(i);
end
for k=2:n
y(k)=0;
% circular shift
for j=2:n
x2(j)=a(j-1);
end
x2(1)=a(n);
for i=1:n
if(i<n+1)

a(i)=x2(i);
y(k)=y(k)+x(i)*a(i);

You might also like