Page Contents

Let us quickly go through Circular Convolution Matlab Code and go through the topic in detail after the article:

We have tried providing multiple codes for Circular Convolution MatLab Code:

Kindly go through the entire article:

ALGORITHM:

Step 1:Â Start
Step 2:Â Read the first sequence
Step 3:Â Read the second sequence
Step 4: Find the length of the first sequence
Step 5: Find the length of the second sequence
Step 6: Perform circular convolution MatLab for both the sequences using inbuilt function
Step 7:Â Plot the axis graph for sequence
Step 8:Â Display the output sequence
Step 9:Â Stop

## Matlab Code for Circular ConvolutionÂ

### Code 1

Â Here is the code
```clc;
close all;
clear all;
x1=input('Enter the first sequence :
');
x2=input('Enter the second sequence
: ');
N1=length(x1);
N2=length(x2);
N=max(N1,N2);
Â
if(N2>N1)
x4=[x1,zeros(1,N-N1)];
x5=x2;
elseif(N2==N1)
Â Â Â Â Â Â Â  x4=x1;
Â Â Â Â Â Â Â  x5=x2;
else
x4=x1;
x5=[x2,zeros(1,N-N2)];
end
Â
x3=zeros(1,N);
for m=0:N-1
x3(m+1)=0;
for n=0:N-1
Â Â Â Â Â Â Â  j=mod(m-n,N);
Â Â Â Â Â Â Â  x3(m+1)=x3(m+1)+x4(n+1).*x5(j+1);
end
end
Â
subplot(4,1,1)
stem(x1);
title('First Input Sequence');
xlabel('Samples');
ylabel('Amplitude');
subplot(4,1,2)
stem(x2);
title('Second Input Sequence');
xlabel('Samples');
ylabel('Amplitude');
subplot(4,1,3)
stem(x3);
title('Circular Convolution Using
Modulo Operator');
xlabel('Samples');
ylabel('Amplitude');
Â
%In built function
y=cconv(x1,x2,N);
subplot(4,1,4)
stem(y);
title('Circular Convolution using
Inbuilt Function');
xlabel('Samples');
ylabel('Amplitude');

```

you can directly execute the code in matlab

I have briefly explained most of the important parts of the code.

Let’s not dive deeply into the details and get a gist of the code.

Code explanation:

First, N1 and N2 we entered 2 sequences.

```x1=input('Enter the first sequence : ');

x2=input('Enter the second sequence : ');```

Took N as the max length of either N1 or N2

`N=max(N1,N2);`

If lengthÂ  (N2>N1)

Then we will be padding zeros to make the length of both the sequences equal.

`x4=[x1,zeros(1,N-N1)];`

If length (N1=N2) then no changes to the sequence.

```elseif(N2==N1)

x4=x1;

x5=x2;```

and if length (N1>N2) then padding the other sequence with zeros.

`x5=[x2,zeros(1,N-N2)];`

Now let’s plot the curve of the input sequences and the output of matlab code for circular convolution

```subplot(4,1,1) stem(x1);

title('First Input Sequence');

xlabel('Samples');

ylabel('Amplitude');

subplot(4,1,2) stem(x2);

title('Second Input Sequence');

xlabel('Samples');

ylabel('Amplitude');

subplot(4,1,3)

stem(x3);

title('Circular Convolution Using Modulo Operator');

xlabel('Samples');

ylabel('Amplitude');```

After plotting all the axis of the graph we will be look using the inbuilt circular convolution MatLab function for performing the code.

`y=cconv(x1,x2,N);`

rest all lines after this are to plot the output curve we get after using the inbuilt circular convolution function in MatLab.

```%In built function y=cconv(x1,x2,N);

subplot(4,1,4)

stem(y);

title('Circular Convolution using Inbuilt Function');

xlabel('Samples');

ylabel('Amplitude');```

If you are looking for Linear Convolution Matlab code check it out here:

Here is the Output
If you guys still have a problem understanding the code, Kindly comment below and we will go through all your queries.

### Code 2:

```

x = input(â€˜enter a sequenceâ€™);

h = input(â€˜enter another sequenceâ€™);

n1=length(x);

n2 = length(h);

n = max(n1,n2);

a=1:n;

x = [x,zeros(1,n-n1)];

h = [h,zeros(1,n-n2)];

y = zeros(1,n);

for i =0:n-1

for j = 0:n-1

k = mod((i-j),n);

y(i+1) = y(i+1) + x(j+1)*h(k+1);

end

end

stem(a,y)

```

Output:

### Code 3:

```MATLAB code
x1=input('Enter the sequence x1(n)=');
x2=input('Enter the sequence x2(n)=');
N=length(x1)+ length(x2)-1;
X1=fft(x1,N);
X2=fft(x2,N);
Y=X1.*X2;
y=ifft(Y);
% y is the linear convolution of x1(n) and x2(n)
disp('Linear convolution of x1(n) and x2(n) is:');
disp(y)
```

### Circular Convolution without using inbuilt function cconv(x,y,n)

It is used to convolve 2 different discrete Fourier transforms.

It is way to fast for long sequences than linear convolutions

### Code 4

```
Enter x(n):
[1 2 2 1]
Enter h(n):
[1 2 3 1]
x(n) is:
1 2 2 1

h(n) is:
1 2 3 1

Y(n) is:
11 9 10 12```

Thus results can be achieved Circular convolution without using cconv(x,y,n)

What is Modulo-n circular convolution?

c = cconv(a,b)
convolves vector ‘a’ and ‘b’
c = cconv(a,b,n)
circularly convolves vectors ‘a’ and ‘b’ where ‘n’ is the length of the resulting vector.
You can also use ‘cconv’ to calculate the circular cross-correlation of two sequences.
Let’s see some examples
```a = [1 2 -1 1];
b = [1 1 2 1 2 2 1 1];

c = cconv(a,b);            % Circular convolution
cref = conv(a,b);          % Linear convolution

dif = norm(c-cref)
```

For more examples visit Mathworks

### What is Circular Convolution:

Circular convolution also known as cyclic convolution to two functions which are aperiodic in nature occurs when one of them is convolved in the normal way with a periodic summation of other function.

A similar situation can be observed can be expressed in terms of a periodic summation of both functions, if the infinite integration interval is reduced to just one period. This situation arises in Discrete-Time Fourier Transform(DTFT) and is called periodic convolution.

Let us considerÂ x toÂ be a function with a defined periodic summation,Â xT, where:

If hÂ is any other function for which the convolutionÂ xTÂ *Â hÂ (Multiply)Â exists, then the convolutionÂ xTÂ âˆ—Â hÂ is periodic and equal to:

whereÂ toÂ is an arbitrary parameter andÂ hTÂ is aÂ periodic summationÂ ofÂ h.

The second integral is called theÂ periodic convolutionÂ of functionsÂ xTÂ andÂ hTÂ & is normalized by 1/T.

WhenÂ xTÂ is expressed as theÂ periodic summationÂ of another function,Â x, the same operation may also be referred to as aÂ circular convolutionÂ of functionsÂ hÂ andÂ x.

Previous articleHow Much is Product Manager Salary?
Next articleCriteria for Choosing a Microcontroller