DSP Singam
DSP Singam
n = -10:10;
unit_sample = (n == 0);
unit_step = (n >= 0);
a = 0.8;
exponential = a.^n;
frequency = 0.1;
sinusoidal = sin(2 * pi * frequency * n);
random_sequence = rand(size(n));
figure;
subplot(3,2,1);
stem(n, unit_sample, 'filled');
title('Unit Sample Sequence');
xlabel('n');
ylabel('x[n]');
subplot(3,2,2);
stem(n, unit_step, 'filled');
title('Unit Step Sequence');
xlabel('n');
ylabel('x[n]');
subplot(3,2,3);
stem(n, exponential, 'filled');
title('Exponential Sequence');
xlabel('n');
ylabel('x[n]');
subplot(3,2,4);
stem(n, sinusoidal, 'filled');
title('Sinusoidal Sequence');
xlabel('n');
ylabel('x[n]');
subplot(3,2,5);
stem(n, random_sequence, 'filled');
title('Random Sequence');
xlabel('n');
ylabel('x[n]');
*pro2
n = -10:10;
x1 = sin(0.2*pi*n);
x2 = cos(0.2*pi*n);
signal_addition = x1 + x2;
signal_multiplication = x1 .* x2;
scaling_factor = 2;
scaled_signal = scaling_factor * x1;
shift_amount = 3;
shifted_signal = circshift(x1, shift_amount);
folded_signal = fliplr(x1);
figure;
subplot(3,2,1);
stem(n, x1, 'filled');
title('Original Signal x1');
xlabel('n');
ylabel('x1[n]');
subplot(3,2,2);
stem(n, x2, 'filled');
title('Original Signal x2');
xlabel('n');
ylabel('x2[n]');
subplot(3,2,3);
stem(n, signal_addition, 'filled');
title('Signal Addition: x1[n] + x2[n]');
xlabel('n');
ylabel('x1[n] + x2[n]');
subplot(3,2,4);
stem(n, signal_multiplication, 'filled');
title('Signal Multiplication: x1[n] * x2[n]');
xlabel('n');
ylabel('x1[n] * x2[n]');
subplot(3,2,5);
stem(n, scaled_signal, 'filled');
title(['Scaled Signal: ' num2str(scaling_factor) ' * x1[n]']);
xlabel('n');
ylabel([num2str(scaling_factor) ' * x1[n]']);
figure;
subplot(2,2,1);
stem(n, shifted_signal, 'filled');
title(['Shifted Signal: x1[n-' num2str(shift_amount) ']']);
xlabel('n');
ylabel(['x1[n-' num2str(shift_amount) ']']);
subplot(2,2,2);
stem(n, folded_signal, 'filled');
title('Folded Signal: x1[-n]');
xlabel('n');
ylabel('x1[-n]');
*pro3
x1 = [1, 2, 3, 4]
x2 = [1, -1, 2]
N1 = length(x1);
N2 = length(x2);
N = N1 + N2 - 1;
x1_padded = [x1, zeros(1, N2-1)]
x2_padded = [x2, zeros(1, N1-1)]
y = zeros(1, N);
for n = 1:N
for k = 1:N1
if (n-k+1 > 0 && n-k+1 <= N2)
y(n) = y(n) + x1_padded(k) * x2_padded(n-k+1);
end
end
end
disp(y)
n_result = 0:N-1;
figure;
subplot(3,1,1);
stem(0:N1-1, x1, 'filled');
title('Sequence x1');
xlabel('n');
ylabel('x1[n]');
subplot(3,1,2);
stem(0:N2-1, x2, 'filled');
title('Sequence x2');
xlabel('n');
ylabel('x2[n]');
subplot(3,1,3);
stem(n_result, y, 'filled');
title('Convolution of x1 and x2');
xlabel('n');
ylabel('y[n]');
*pro4
a = [1 -0.9];
b = 1;
[H, z] = tf2zpk(b, a);
figure;
zplane(b, a);
title('Pole-Zero Plot of H(z)');
xlabel('Real Part');
ylabel('Imaginary Part');
[H_ejw, w] = freqz(b, a, 'whole');
figure;
subplot(2,1,1);
plot(w/pi, abs(H_ejw));
title('Magnitude Response |H(e^{j\omega})|');
xlabel('\omega / \pi');
ylabel('|H(e^{j\omega})|');
subplot(2,1,2);
plot(w/pi, angle(H_ejw));
title('Phase Response ?H(e^{j\omega})');
xlabel('\omega / \pi');
ylabel('Phase (radians)');
n = 0:20;
h = filter(b, a, [1 zeros(1, length(n)-1)]);
figure;
stem(n, h, 'filled');
title('Impulse Response h(n)');
xlabel('n');
ylabel('h[n]');
*Pro5
x = [1, 2, 3, 4];
N = length(x);
x = [x, zeros(1, N-length(x))];
X = zeros(1, N);
for k = 0:N-1
for n = 0:N-1
X(k+1) = X(k+1) + x(n+1) * exp(-1i * 2 * pi * k * n / N);
end
end
disp('the N poiny DFT is:')
disp(X)
k = 0:N-1;
magnitude_spectrum = abs(X);
phase_spectrum = angle(X);
figure;
subplot(2,1,1);
stem(k, magnitude_spectrum, 'filled');
title('Magnitude Spectrum');
xlabel('Frequency Index k');
ylabel('|X(k)|');
subplot(2,1,2);
stem(k, phase_spectrum, 'filled');
title('Phase Spectrum');
xlabel('Frequency Index k');
ylabel('?X(k) (radians)');
*pro6
x1 = [1, 2, 3, 4];
x2 = [1, 2, 1, 2];
N1 = length(x1);
N2 = length(x2);
N = max(N1, N2);
x1 = [x1, zeros(1, N-N1)];
x2 = [x2, zeros(1, N-N2)];
X1 = zeros(1, N);
X2 = zeros(1, N);
for k = 0:N-1
for n = 0:N-1
X1(k+1) = X1(k+1) + x1(n+1) * exp(-1i * 2 * pi * k * n / N);
X2(k+1) = X2(k+1) + x2(n+1) * exp(-1i * 2 * pi * k * n / N);
end
end
disp('The DFT of X1 is :')
disp('The DFT of X2 is :')
disp(X1)
disp(X2)
Y_circular = X1 .* X2;
y_circular = zeros(1, N);
for n = 0:N-1
for k = 0:N-1
y_circular(n+1) = y_circular(n+1) + Y_circular(k+1) * exp(1i * 2 * pi * k * n /
N);
end
end
y_circular = real(y_circular) / N;
N_linear = N1 + N2 - 1;
x1_padded = [x1, zeros(1, N_linear-N)];
x2_padded = [x2, zeros(1, N_linear-N)];
X1_padded = zeros(1, N_linear);
X2_padded = zeros(1, N_linear);
for k = 0:N_linear-1
for n = 0:N_linear-1
X1_padded(k+1) = X1_padded(k+1) + x1_padded(n+1) * exp(-1i * 2 * pi * k * n /
N_linear);
X2_padded(k+1) = X2_padded(k+1) + x2_padded(n+1) * exp(-1i * 2 * pi * k * n /
N_linear);
end
end
Y_linear = X1_padded .* X2_padded;
y_linear = zeros(1, N_linear);
for n = 0:N_linear-1
for k = 0:N_linear-1
y_linear(n+1) = y_linear(n+1) + Y_linear(k+1) * exp(1i * 2 * pi * k * n /
N_linear);
end
end
y_linear = real(round(y_linear)) / N_linear;
figure;
subplot(2, 1, 1);
stem(0:N-1, y_circular, 'filled');
title('Circular Convolution using DFT and IDFT');
xlabel('n');
ylabel('y_{circular}[n]');
subplot(2, 1, 2);
stem(0:N_linear-1, y_linear, 'filled');
title('Linear Convolution using DFT and IDFT');
xlabel('n');
ylabel('y_{linear}[n]');
*pro 7
% Given sequences
x1 = [1, 2, 3, 4]; % Example sequence 1
x2 = [4, 3, 2, 1]; % Example sequence 2
N = length(x1); % Length of the sequences
% Verify the linearity property of DFT
% Linearity: DFT{a*x1 + b*x2} = a*DFT{x1} + b*DFT{x2}
a = 2;
b = 3;
% Compute the DFTs manually
X1 = fft(x1);
X2 = fft(x2);
% Left-hand side of the linearity property
lhs = fft(a*x1 + b*x2);
% Right-hand side of the linearity property
rhs = a * X1 + b * X2;
% Display results for linearity property
disp('Linearity Property Verification:');
disp('LHS (DFT of a*x1 + b*x2):');
disp(lhs);
disp('RHS (a*DFT{x1} + b*DFT{x2}):');
disp(rhs);
% Verify the circular time shift property of DFT
% Circular time shift: If y[n] = x[(n-m) mod N], then DFT{y[n]} = W^km * DFT{x[n]}
m = 1; % Time shift amount
y = circshift(x1, [0, m]);
% Compute the DFTs
Y = fft(y);
% Compute the circular time shift factor W^km
W = exp(-1i * 2 * pi * m / N);
shift_factor = W .^ (0:N-1);
% Right-hand side of the circular time shift property
rhs_time_shift = shift_factor .* X1;
% Display results for circular time shift property
disp('Circular Time Shift Property Verification:');
disp('LHS (DFT of shifted sequence y):');
disp(Y);
disp('RHS (W^km * DFT{x}):');
disp(rhs_time_shift);
% Verify the circular frequency shift property of DFT
% Circular frequency shift: If Y[k] = X[k-k0 mod N], then y[n] = W^k0n * x[n]
k0 = 1; % Frequency shift amount
Y_shifted = circshift(X1, [0, k0]);
% Compute the circular frequency shift factor W^k0n
shift_factor_freq = exp(1i * 2 * pi * k0 * (0:N-1) / N);
% Right-hand side of the circular frequency shift property
rhs_freq_shift = ifft(Y_shifted);
% Left-hand side is the original sequence multiplied by the frequency shift factor
lhs_freq_shift = shift_factor_freq .* x1;
% Display results for circular frequency shift property
disp('Circular Frequency Shift Property Verification:');
disp('LHS (shift_factor_freq .* x1):');
disp(lhs_freq_shift);
disp('RHS (ifft of shifted DFT):');
disp(rhs_freq_shift);
% Plot results
figure;
subplot(3, 1, 1);
stem(abs(lhs), 'filled');
title('Magnitude of LHS (DFT of a*x1 + b*x2)');
subplot(3, 1, 2);
stem(abs(rhs), 'filled');
title('Magnitude of RHS (a*DFT{x1} + b*DFT{x2})');
subplot(3, 1, 3);
stem(abs(lhs - rhs), 'filled');
title('Difference (LHS - RHS)');
figure;
subplot(3, 1, 1);
stem(abs(Y), 'filled');
title('Magnitude of LHS (DFT of time-shifted sequence)');
subplot(3, 1, 2);
stem(abs(rhs_time_shift), 'filled');
title('Magnitude of RHS (W^km * DFT{x})');
subplot(3, 1, 3);
stem(abs(Y - rhs_time_shift), 'filled');
title('Difference (LHS - RHS)');
figure;
subplot(3, 1, 1);
stem(abs(lhs_freq_shift), 'filled');
title('Magnitude of LHS (shift_factor_freq .* x1)');
subplot(3, 1, 2);
stem(abs(rhs_freq_shift), 'filled');
title('Magnitude of RHS (ifft of shifted DFT)');
subplot(3, 1, 3);
stem(abs(lhs_freq_shift - rhs_freq_shift), 'filled');
title('Difference (LHS - RHS)');
*Pro 8
x = [1, 2, 3, 4, 5, 6, 7, 8];
X = dit_radix2_fft(x);
disp('Input sequence:');
disp(x);
function X = dit_radix2_fft(x)
N = length(x);
if N == 1
X = x;
return;
end
if mod(N, 2) ~= 0
error('The length of the input sequence must be a power of 2.');
end
x_even = x(1:2:N);
x_odd = x(2:2:N);
X_even = dit_radix2_fft(x_even);
X_odd = dit_radix2_fft(x_odd);
X = zeros(1, N);
for k = 1:N/2
twiddle_factor = exp(-2j * pi * (k-1) / N);
X(k) = X_even(k) + twiddle_factor * X_odd(k);
X(k + N/2) = X_even(k) - twiddle_factor * X_odd(k);
end
end
*pro9
fs = 1000;
fc = 100;
N = 50;
wc = 2 * pi * fc / fs;
n = 0:N;
hd = (wc/pi) * sinc(wc * (n - N/2) / pi);
h = hd .* hamming(N+1)';
disp('FIR Filter Coefficients:');
disp(h);
[H, f] = freqz(h, 1, 1024, fs);
figure;
plot(f, abs(H));
title('Magnitude Response of the FIR Low-Pass Filter');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;
figure;
stem(n, h);
title('Impulse Response of the FIR Low-Pass Filter');
xlabel('Sample index (n)');
ylabel('Amplitude');
grid on;
t = 0:1/fs:1;
x = sin(2*pi*50*t) + sin(2*pi*200*t);
y = filter(h, 1, x);
figure;
subplot(2,1,1);
plot(t, x);
title('Original Signal (Sum of 50 Hz and 200 Hz)');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
subplot(2,1,2);
plot(t, y);
title('Filtered Signal (After FIR Low-Pass Filtering)');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
*pro10
% Filter specifications
fs = 1000; % Sampling frequency (Hz)
fc = 100; % Cutoff frequency (Hz)
N = 50; % Filter order (number of taps - 1)
window_type = 'hamming'; % Window type ('hamming', 'hanning', 'blackman')
% Design the high-pass FIR filter
h =highpass_fir_filter(fs, fc, N,window_type);
% Display the filter coefficients
disp('High-Pass FIR Filter Coefficients:');
disp(h);
% Frequency response of the filter
[H, f] = freqz(h, 1, 1024, fs);
% Plot the magnitude response of the filter
figure;
plot(f, abs(H));
title('Magnitude Response of the FIR High-Pass Filter');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;
% Plot the impulse response of the filter
figure;
stem(0:N, h);
title('Impulse Response of the FIR High-Pass Filter');
xlabel('Sample index (n)');
ylabel('Amplitude');
grid on;
% Generate a test signal (sum of two sinusoids)
t = 0:1/fs:1; % 1 second time vector
x = sin(2*pi*50*t) + sin(2*pi*200*t); % Sum of 50 Hz and 200 Hz signals
% Filter the test signal
y = filter(h, 1, x);
% Plot the original and filtered signals
figure;
subplot(2,1,1);
plot(t, x);
title('Original Signal (Sum of 50 Hz and 200 Hz)');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
subplot(2,1,2);
plot(t, y);
title('Filtered Signal (After FIR High-Pass Filtering)');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
*pro11
% Filter specifications
fs = 1000; % Sampling frequency (Hz)
fc = 100; % Cutoff frequency (Hz)
N = 50; % Filter order (number of taps - 1)
window_type = 'hamming'; % Window type ('hamming', 'hanning', 'blackman')
% Design the high-pass FIR filter
h =highpass_fir_filter(fs, fc, N,window_type);
% Display the filter coefficients
disp('High-Pass FIR Filter Coefficients:');
disp(h);
% Frequency response of the filter
[H, f] = freqz(h, 1, 1024, fs);
% Plot the magnitude response of the filter
figure;
plot(f, abs(H));
title('Magnitude Response of the FIR High-Pass Filter');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;
% Plot the impulse response of the filter
figure;
stem(0:N, h);
title('Impulse Response of the FIR High-Pass Filter');
xlabel('Sample index (n)');
ylabel('Amplitude');
grid on;
% Generate a test signal (sum of two sinusoids)
t = 0:1/fs:1; % 1 second time vector
x = sin(2*pi*50*t) + sin(2*pi*200*t); % Sum of 50 Hz and 200 Hz signals
% Filter the test signal
y = filter(h, 1, x);
% Plot the original and filtered signals
figure;
subplot(2,1,1);
plot(t, x);
title('Original Signal (Sum of 50 Hz and 200 Hz)');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
subplot(2,1,2);
plot(t, y);
title('Filtered Signal (After FIR High-Pass Filtering)');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
*pro12
fs = 1000;
fc = 100;
order = 4;
Wn = 2 * fc / fs;
[b, a] = butter(order, Wn, 'high');
disp('Butterworth High-Pass Filter Coefficients:');
disp('Numerator coefficients (b):');
disp(b);
disp('Denominator coefficients (a):');
disp(a);
[H, f] = freqz(b, a, 1024, fs);
figure;
plot(f, abs(H));
title('Magnitude Response of the Butterworth High-Pass Filter');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;
figure;
plot(f, angle(H));
title('Phase Response of the Butterworth High-Pass Filter');
xlabel('Frequency (Hz)');
ylabel('Phase (radians)');
grid on;
t = 0:1/fs:1;
x = sin(2*pi*50*t) + sin(2*pi*200*t);
y = filter(b, a, x);
figure;