0% found this document useful (0 votes)
4 views7 pages

Exp 2,3

The document contains MATLAB code for performing various signal processing operations, including cross-correlation, auto-correlation, linear and circular convolution, up-sampling, and interpolation. It provides step-by-step instructions for inputting sequences and visualizing the results through plots. The code is designed for educational purposes, as indicated by the inclusion of a student ID.

Uploaded by

riadislam95479
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Exp 2,3

The document contains MATLAB code for performing various signal processing operations, including cross-correlation, auto-correlation, linear and circular convolution, up-sampling, and interpolation. It provides step-by-step instructions for inputting sequences and visualizing the results through plots. The code is designed for educational purposes, as indicated by the inclusion of a student ID.

Uploaded by

riadislam95479
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

2.

Program Code (a): Correlation Sequences, Cross-Correlation of the Sequences:

clc;
clear all;
close all;

disp('Student ID: 22702033');

x = input('Enter the sequence 1: ');


h = input('Enter the sequence 2: ');

y = xcorr(x, h);
figure;

subplot(3,1,1);
stem(x);
xlabel('n ->');
ylabel('Amplitude ->');
title('Input sequence 1');

subplot(3,1,2);
stem(h);
xlabel('n ->');
ylabel('Amplitude ->');
title('Input sequence 2');

subplot(3,1,3);
stem(fliplr(y));
xlabel('n ->');
ylabel('Amplitude ->');
title('Output (Cross-correlation) sequence');

disp('The resultant cross-correlation sequence is:');


fliplr(y)
Auto Correlation Function:
clc;
close all;
clear all;

disp('Student ID: 22702033');

x = input('Enter the sequence: ');


y = xcorr(x, x);

figure;

subplot(2,1,1);
stem(x);
ylabel('Amplitude ->');
xlabel('n ->');
title('Input sequence');

subplot(2,1,2);
stem(fliplr(y));
ylabel('Amplitude ->');
xlabel('n ->');
title('Auto-correlation Output sequence');

disp('The resultant auto-correlation sequence is:');


fliplr(y)
Programme code(b): Linear Convolution and Circular Convoluatoin:

% linear convolution
close all
clear all
x=input('Enter x: ')
h=input('Enter h: ')
m=length(x);
n=length(h); X=[x zeros(1,n)];
H=[h zeros(1,m)];
for i=1:n+m-1
Y(i)=0;
for j=1:i
Y(i)=Y(i)+X(j)*H(i-j+1);
end
end
Y
stem(Y); ylabel('Y[n]');
xlabel('>n');
title('Convolution of Two Signals without convfunction');

Enter x: [4 5 6 7 8]
Enter h: [1 2 3 4]
Circular Convoluatoin:
% Define two input signals
x = input('Enter The sequence x(n): ');
h = input('Enter The sequence h(n): ');

% Length of the signals


Nx = length(x);
Nh = length(h);

% Length of the result


N = Nx + Nh - 1;

% Zero-padding the signals to the length of the result


x_padded = [x, zeros(1, N - Nx)];
h_padded = [h, zeros(1, N - Nh)];

% Initialize the result


y = zeros(1, N);

% Circular convolution
for n = 1:N
for k = 1:N
y(n) = y(n) + x_padded(k) * h_padded(mod(n - k, N) + 1);
end
end

% Display the result


disp('Circular Convolution Result:');
disp(y);

stem(y);
title('Circular Convolution');
xlabel('n');
ylabel('y(n)');

Enter The sequence x(n): [4 7 9 6 7]


Enter The sequence h(n): [1 2 3 4]
Circular Convolution Result:
4 15 35 61 74 68 45 28
3. Program Code(a): Effect of up-sampling in frequency domain:

clc;
close all;
clear all;

disp('Student ID: 22702033');

freq = [0 0.45 0.5 1];


mag = [0 1 0 0];

x = fir2(99, freq, mag);

[Xz, W] = freqz(x, 1, 512);

figure(1);
plot(W/pi, abs(Xz));
grid on;
xlabel('Omega / Pi');
ylabel('Magnitude');
title('Input Spectrum');

L = input('Enter the up-sampling factor: ');

% Up-sampling operation
y = zeros(1, L * length(x));
y(1:L:end) = x;

[Vz, W] = freqz(y, 1, 512);

figure(2);
plot(W/pi, abs(Vz));
grid on;
xlabel('Omega / Pi');
ylabel('Magnitude');
title('Output Spectrum (After Up-sampling)');

Enter the up-sampling factor: 5


Program Code: (b) The effect of Interpolation process:

clc;
close all;
clear all;
N = input('Length of input signal: ');
L = input('Up-sampling factor: ');
f1 = input('Frequency of first sinusoid: ');
f2 = input('Frequency of second sinusoid: ');
n = 0:N-1;
x = sin(2*pi*f1*n) + sin(2*pi*f2*n); % Generate the input sequence
y = interp(x,L); % Generate interpolated o/p
figure(1); % Plot input & output sequences
stem(n,x(1:N));
title('Input sequence');
xlabel('Time index n');
ylabel('Amplitude');
grid on;
m=0:N*L-1;
figure(2);
stem(m,y(1:N*L));
title('Interpolated Output sequence');
xlabel('Time index n');
ylabel('Amplitude');
grid on;
Length of input signal: 90
Up-sampling factor: 2
Frequency of first sinusoid: 50
Frequency of second sinusoid: 100

You might also like