Signal Processing: Convolution, DFT, Filters, and Signals

Linear and Circular Convolution Using Circshift

This section demonstrates linear and circular convolution using the circshift function in MATLAB.

clc;
clear all;
close all;
x = [1 2 3];
h = [4 5];
L = length(x);
M = length(h);
N = L + M - 1;
x_padded = [x, zeros(1, N - L)];
h_padded = [h, zeros(1, N - M)];
for i = 1:N
    c = circshift(h_padded', i - 1);
    d(:, i) = c;
end
o = d * x_padded.';
disp('Linear convolution without function');
disp(o');
subplot(3, 1, 1);
stem(x, '*');
xlabel('n');
ylabel('Magnitude');
title('Input x');
subplot(3, 1, 2);
stem(h, 'x');
xlabel('n');
ylabel('Magnitude');
title('Input h');
subplot(3, 1, 3);
stem(o, 'o');
xlabel('n');
ylabel('Magnitude');
title('Output y');

Circular Convolution Property of a DFT Sequence

This demonstrates the circular convolution property of the Discrete Fourier Transform (DFT).

x = [1 2 1 2];
h = [2 4 6 8];
N = max(length(x), length(h));
X = fft(x, N);
H = fft(h, N);
Y = X .* H;
y_RHS = ifft(Y);
y_RHS_New = y_RHS;
disp('RHS (Frequency-domain Circular Convolution):');
disp(y_RHS);
L = length(x);
M = length(h);
N = max(L, M);
x_padded = [x, zeros(1, N - L)];
h_padded = [h, zeros(1, N - M)];
d = zeros(N, N);
for i = 1:N
    c = circshift(h_padded, i - 1);
    d(:, i) = c;
end
y_LHS = d * x_padded.';
if isequal(real(y_LHS), real(y_RHS_New))
    disp('Verification successful: Convolution in time domain equals multiplication in frequency domain.');
else
    disp('Verification failed: Results do not match.');
end
disp('LHS (Time-domain Circular Convolution):');
disp(y_LHS);
disp('RHS (Frequency-domain Circular Convolution):');
disp(y_RHS);

Linearity Property of DFT

This code verifies the linearity property of the DFT.

clc;
clear all;
close all;
x1 = [1, 2, 3];
x2 = [4, 5];
a = 2;
b = 3;
L = length(x1);
M = length(x2);
N = L + M - 1;
x1_padded = [x1, zeros(1, N - L)];
x2_padded = [x2, zeros(1, N - M)];
linear_combination = a * x1_padded + b * x2_padded;
LHS = fft(linear_combination);
X1 = fft(x1_padded);
X2 = fft(x2_padded);
RHS = a * X1 + b * X2;
if isequal(LHS, RHS)
    disp('Verification successful: DFT is linear in nature.');
else
    disp('Verification failed: DFT does not exhibit linearity.');
end
disp('DFT of linear combination:');
disp(LHS);
disp('RHS:');
disp(RHS);

Signal Generation

This section shows how to generate and plot signals in MATLAB.

clc;
clear all;
close all;
t = 0:0.01:50;
f = 0.5;
x = sin(2 * pi * f * t);
plot(t, x);
title('Sinusoidal Signal');
x1 = exp(-0.05 * t) .* x;
figure;
plot(t, x1);
title('Damped Sinusoidal Signal');

Parseval’s Theorem

This code verifies Parseval’s theorem, which relates the energy of a signal in the time domain to its energy in the frequency domain.

clc;
clear all;
close all;
x = [1, 2, 3, 4];
N = length(x);
X = fft(x);
LHS = sum(abs(x .* x));
RHS = (1 / N) * sum(abs(X .* X));
disp('Sum of squares in the time domain');
disp(LHS);
disp('Sum of squares in the frequency domain');
disp(RHS);
if isequal(LHS, RHS)
    disp('Parseval\'s theorem is verified: Both sides are equal.');
else
    disp('Verification failed: There is a discrepancy between the two sides.');
end

Square Wave with Duty Cycle

This generates a square wave with a specified duty cycle.

clc;
clear all;
close all;
f = 1;
t = -1:0.1:1;
duty_cycle = 50;
e = square(2 * pi * f * t, duty_cycle);
plot(t, e);
xlabel('Time (s)');
ylabel('Amplitude');
title(['Square Wave with ', num2str(duty_cycle), '% Duty Cycle']);
grid on;

FIR Filter Design

This section demonstrates how to design a Finite Impulse Response (FIR) filter using the fir1 function and analyze its frequency response.

clc;
clear all;
close all;
wc = 0.5 * pi;
N = 25;
b = fir1(N, wc / pi, hamming(N + 1));
disp(b);
w = 0:0.01:pi;
H = freqz(b, 1, w);
subplot(2, 2, 1);
plot(w / pi, 20 * log10(abs(H)));
grid on;
title('Frequency Response');
xlabel('Frequency');
ylabel('Magnitude (dB)');
subplot(2, 2, 2);
plot(w / pi, angle(H));
grid on;
title('Phase Response');
xlabel('Frequency');
ylabel('Phase Angle');

Note: There appears to be a duplicated line of code in the original snippet: (b=fir1(N,wc/pi,high, hamming(N+1));). This has been corrected in the code above, assuming a low-pass filter was intended. If a high-pass filter is desired, change the call to fir1 accordingly.

Combined Signal Generation

This code generates a signal composed of two sinusoidal components with different frequencies.

clc;
clear all;
close all;
Fs = 10000; % Define a sampling rate
t = 0:1/Fs:1;
f1 = 300;
f2 = 1500;
signal1 = sin(2 * pi * f1 * t);
signal2 = sin(2 * pi * f2 * t);
signal = signal1 + signal2;
subplot(3,1,1);
plot(t, signal1);
title('300 Hz Signal');
xlabel('Time (seconds)');
ylabel('Amplitude');
subplot(3,1,2);
plot(t, signal2);
title('1500 Hz Signal');
xlabel('Time (seconds)');
ylabel('Amplitude');
subplot(3,1,3);
plot(t, signal);
title('Combined Signal (300 Hz + 1500 Hz)');
xlabel('Time (seconds)');
ylabel('Amplitude');

Note: Added a sampling frequency (Fs) to make the time vector ‘t’ more realistic.

Linear Convolution using DFT

This demonstrates linear convolution using the DFT.

clc;
clear all;
close all;
n1 = input('Enter the length of the sequence 1: ');
x = input('Enter the sequence 1: ');
n2 = input('Enter the length of the sequence 2: ');
h = input('Enter the sequence 2: ');
x = [x, zeros(1, n2 - 1)];
h = [h, zeros(1, n1 - 1)];
X = fft(x);
H = fft(h);
Y = X .* H; % Corrected: Element-wise multiplication for circular convolution
y = ifft(Y);
subplot(3, 1, 1);
stem(x);
title('Sequence 1');
subplot(3, 1, 2);
stem(h);
title('Sequence 2');
subplot(3, 1, 3);
stem(y);
title('Linear Convolution using DFT');

Note: The original code had an error in the convolution calculation (Y=X*H). For the DFT method of linear convolution, element-wise multiplication (.*) should be used, followed by ifft. Also corrected the zero-padding to ensure correct results.

Plotting Basic Signals

This section demonstrates how to plot basic signals like unit step, signum, etc.

clc;
close all;
clear all;
t = -10:10;
k = [zeros(1, 10), 1, zeros(1, 10)];
subplot(2, 2, 1);
stem(t, k);
title('Unit Step');
s = [-1 * ones(1, 10), ones(1, 11)];
subplot(2, 2, 2);
stem(t, s);
title('Signum Function');
X = (t >= 5);
subplot(2, 2, 3);
stem(t, X);
title('Delayed Step (t >= 5)');
Y = (t >= 0) * 2 - 1;
subplot(2, 2, 4);
stem(t, Y);
title('Another Signum Representation');

Note: Corrected the definition of the ‘s’ (signum) function to have the correct length (21 elements) to match the time vector ‘t’. Also, improved the titles and subplot arrangement.