Dsplab
Dsplab
Work Sheet-4
Circular Folding, Circular Shifting, and Circular Convolution
AIM:-
To realise the following operations in MATLAB
1. Circular Folding
2. Circular Shifting
3. Circular Convolution
QUESTIONS :-
1. Consider the signal 𝑥[𝑛] = 10(0.8) ^𝑛 defined in the
interval 0 ≤ n ≤ 10.
(a) Write a MATLAB function called cfold_ROLLNO to
implement the circular folding operation (Hint: Take x[n], n
and N as inputs).
(b) Using the function defined in part (a), determine and
plot x [((−n))11]
CODE:
clc;
clear;
close;
n=0:10;
x=10*(0.8).^(n);
CIRCULAR FOLDING FUNCTION N=length(n);
a=cfold_ECE22173(x,n,N);
function y=cfold_ECE22173(x,n,N)
for i =1:N stem(n,a);
y(i)=x(mod(-n(i),N)+1); xlabel('n_ ECE22173');
end ylabel('a_ ECE22173');
title('Circular Folding of x[n]_ ECE22173');
%% 1 b)
clc;
clear;
close;
n=0:10;
x=10*(0.8).^(-n);
N=length(n);
a=cfold_ECE22173(x,n,N);
stem(n,a);
xlabel('n_ ECE22173');
ylabel('a_ ECE22173');
title('Circular Folding of x[-n]_ ECE22173');
CODE:-
%% 2a),b)
clc;
clear;
close;
n=0:10;
CIRCULAR SHIFT FUNCTION:- n0=-4;
x=10*(0.8).^(n);
function y=cshift_ECE22173(x,n,n0) a=cshift_ECE22173(x,n,n0);
N=length(n); stem(n,a);
for i =1:N xlabel('n_ ECE22173');
y(i)=x(mod(n(i)-n0,N)+1); ylabel('a_ ECE22173');
end title('Circular Shifting of x[-n]_ ECE22173');
%% 2c)
clc;
clear;
close;
n=0:15;
n0=+3;
x=(10*(0.8).^(n)).*(n>=0 & n<=10)+ 0.*(n>=11 &n<=15);
a=cshift_ECE22173(x,n,n0);
stem(n,a);
xlabel('n_ ECE22173');
ylabel('a_ ECE22173');
title('Circular Shifting of x[n-3]_ ECE22173');
%% 2d)
clc;
clear;
close;
n=0:15;
n0=+6;
x=(10*(0.8).^(n)).*(n>=0 & n<=10)+ 0.*(n>=11 &n<=15);
a=cshift_ECE22173(x,n,n0);
stem(n,a);
xlabel('n_ ECE22173');
ylabel('a_ ECE22173');
title('Circular Shifting of x[n-6]_ ECE22173');
%% 3
clc;
clear;
close;
n=0:3;
x=[1,2,2,0];
h=[1,2,3,4];
ind=1;
a=cconv_time_freq_ECE22173(x,h,4,ind);
b=cconv(x,h,length(x));
stem(n,a);
xlabel('n_ ECE22173');
ylabel('a_ ECE22173');
title('Circular Convolution using function
ECE22173');
figure;
stem(n,b);
xlabel('n_ ECE22173');
ylabel('b_ ECE22173');
title('Circular Convolution using cconv ECE22173');
INPUT:-
>>Enter length of block: 4 DATE:30/03/2024
Work Sheet-5
Linear Filtering using Overlap add and Overlap save methods
ECE22173 h[n] is: 1. Compute the block convolution using overlap add
1 2 3 -1
method for the input sequence x[n] = [1 2 -1 2 3 -2 -3 -1 1 1
output y[n] is: 2 -1] and the impulse response h[n] = [ 1 2 3 -1]. Assume
1.0000 4.0000 6.0000 input signal block size to be 4. Compare the obtained result
5.0000 2.0000 11.0000 0.0000 with that obtained from the inbuilt function conv.
-16.0000 -8.0000 3.0000 8.0000
5.0000 3.0000 -5.0000 1.0000
CODE:-
x_n = [1,2,-1,2,3,-2,-3,-1,1,1,2,-1];
h_n = [1,2,3,-1];
L = input("Enter length of block: ");
M = length(h_n);
B = round(length(x_n)/L);
h1 = [h_n, zeros(1, L-1)];
x_i = zeros(B, L+M-1);
y_i = zeros(B,L+M-1);
for i=1:B
x_i(i,:) = [x_n((i-1)*L+1:i*L), zeros(1,M-1)];
y_i(i,:) = cconv(x_i(i,:), h1,L+M-1);
end
y_n = [];
for i=1:B
if(i==1)
y_n = [y_n,y_i(i,1:L), y_i(i,L+1:L+M-
1)+y_i(i+1,1:M-1)];
elseif(i==B)
y_n = [y_n,y_i(i,M:L+M-1)];
else
y_n = [y_n,y_i(i,M:L), y_i(i,L+1:L+M-
1)+y_i(i+1,1:M-1)];
end
end
disp('ECE22173 x[n] is:');
disp(x_n);
disp('ECE22173 h[n] is:');
disp(h_n);
disp('output y[n] is:')
disp(y_n);
2. Perform block convolution for the sequences given in
INPUT:- Qn.1 using overlap save method with same input signal
>>Enter lenght of block: 4
block size and verify the result
CODE:-
OUTPUT:-
x_n = [1,2,-1,2,3,-2,-3,-1,1,1,2,-1];
ECE22173 x[n] is: h_n = [1,2,3,-1];
1 2 -1 2 3 -2 L = input("Enter length of block");
-3 -1 1 1 2 -1 M = length(h_n);
B = round(length(x_n)/L)+1;
ECE22173 h[n] is: h1 = [h_n, zeros(1,L-1)];
1 2 3 -1 x_1 = zeros(B,L+M-1);
y_1 = zeros(B, L+M-1);
output y[n] is: for i=1:B
Columns 1 through 15 if(i==1)
x_i(i,:) = [zeros(1, M-1), x_n(1:L)];
1.0000 4.0000 6.0000 elseif(i==B)
5.0000 2.0000 11.0000 -0.0000 x_i(i,:) = [x_i(i-1,L+1:L+M-1), zeros(1,L)];
-16.0000 -8.0000 3.0000 8.0000 else
5.0000 3.0000 -5.0000 1.0000 x_i(i,:) = [x_i(i-1,L+1:L+M-1),x_n((i-
1)*L+1:i*L)];
end
y_i(i,:) = cconv(x_i(i,:), h1, L+M-1);
end
y_n = [];
for i=1:B
y_n = [y_n,y_i(i,M:L+M-1)];
end
disp('ECE22173 x[n] is:');
disp(x_n);
disp('ECE22173 h[n] is:');
disp(h_n);
disp('output y[n] is:');
disp(y_n);