2. frequency response of a system defined by H(z)=1/(1−0.
5z−1)
% Define symbolic variable for z
syms z omega
% Define the system function H(z)
H_z = 1 / (1 - 0.5 * z^(-1));
% Convert to frequency domain by substituting z = e^(jω)
H_w = subs(H_z, z, exp(1j * omega));
% Display the frequency response equation
disp('Frequency Response Equation:');
pretty(H_w) % Prints the equation in a readable format
% Convert to a function for numerical evaluation
b = [1]; % Numerator coefficients
a = [1, -0.5]; % Denominator coefficients
[H, w] = freqz(b, a, 1024);
% Print frequency response values
disp(' ');
disp('Frequency Response Values:');
disp('---------------------------------');
disp('Frequency (rad/sample) | Magnitude | Phase (radians)');
disp('---------------------------------');
for i = 1:10:length(w) % Print every 10th value to reduce output length
fprintf('%8.4f | %8.4f | %8.4f\n', w(i), abs(H(i)), angle(H(i)));
end
% Plot magnitude and phase response
figure;
subplot(2,1,1);
plot(w, abs(H), 'b', 'LineWidth', 1.5);
title('Magnitude Response');
xlabel('Frequency (rad/sample)');
ylabel('|H(e^{jω})|');
grid on;
subplot(2,1,2);
plot(w, angle(H), 'r', 'LineWidth', 1.5);
title('Phase Response');
xlabel('Frequency (rad/sample)');
ylabel('Phase (radians)');
grid on;
5. Simulating Aliasing
% Define the parameters
f_signal = 2000; % 2 kHz sine wave
fs_orig = 10000; % High sampling rate for continuous signal (10 kHz)
t_orig = 0:1/fs_orig:0.01; % Time vector for original signal
x_cont = sin(2 * pi * f_signal * t_orig); % Original continuous signal
% Undersampling at 1.5 kHz (below Nyquist rate)
fs1 = 1500; % Sampling rate (1.5 kHz)
t1 = 0:1/fs1:0.01; % Time vector for undersampled signal
x1 = sin(2 * pi * f_signal * t1); % Undersampled signal
% Oversampling at 4 kHz (above Nyquist rate)
fs2 = 4000; % Sampling rate (4 kHz)
t2 = 0:1/fs2:0.01; % Time vector for properly sampled signal
x2 = sin(2 * pi * f_signal * t2); % Properly sampled signal
% Reconstruct the signal using sinc interpolation (ideal reconstruction)
t_interp = 0:1/fs_orig:0.01; % High-resolution time vector for interpolation
x_recon = zeros(size(t_interp));
for i = 1:length(t_interp)
% Ideal interpolation (sinc function) for reconstruction
x_recon(i) = sum(x1 .* sinc((t_interp(i) - t1) * fs1));
end
% Plot the results
figure;
% Plot the original continuous signal
subplot(4,1,1);
plot(t_orig, x_cont, 'k', 'LineWidth', 1.5);
title('Original Continuous 2 kHz Sine Wave');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
% Plot the undersampled signal (aliasing occurs)
subplot(4,1,2);
stem(t1, x1, 'r', 'LineWidth', 1.5);
title('Undersampled Signal at 1.5 kHz (Aliasing)');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
% Plot the properly sampled signal (no aliasing)
subplot(4,1,3);
stem(t2, x2, 'g', 'LineWidth', 1.5);
title('Properly Sampled Signal at 4 kHz');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
% Plot the reconstructed signal from undersampled data
subplot(4,1,4);
plot(t_interp, x_recon, 'b--', 'LineWidth', 1.5);
title('Reconstructed Signal (Sinc Interpolation)');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
6. Compute and Plot DTFT of sinc(n/5)
% Define the sequence x[n] = sinc(n/5)
n = -50:50; % Discrete time indices (you can adjust the range as needed)
x = sinc(n/5); % sinc function definition
% Compute the DTFT using a frequency vector (discretized)
omega = linspace(-pi, pi, 1000); % Frequency range from -pi to pi
X_omega = zeros(size(omega)); % Initialize the DTFT result
% Compute the DTFT by summing the Fourier transform expression
for k = 1:length(omega)
X_omega(k) = sum(x .* exp(-1j * omega(k) * n)); % DTFT calculation
end
% Plot the magnitude of the DTFT
figure;
subplot(2, 1, 1);
plot(omega, abs(X_omega), 'LineWidth', 1.5);
title('Magnitude of DTFT of x[n] = sinc(n/5)');
xlabel('Frequency (\omega)');
ylabel('Magnitude');
grid on;
% Plot the phase of the DTFT
subplot(2, 1, 2);
plot(omega, angle(X_omega), 'LineWidth', 1.5);
title('Phase of DTFT of x[n] = sinc(n/5)');
xlabel('Frequency (\omega)');
ylabel('Phase (radians)');
grid on;
7. Generate a finite-duration sinusoidal signal and compute its DFS
% Parameters for the finite-duration sinusoidal signal
N = 32; % Number of samples
f = 5; % Frequency of the sinusoid (in Hz)
fs = 32; % Sampling frequency (in Hz)
n = 0:N-1; % Sample indices
% Generate finite-duration sinusoidal signal
x = sin(2 * pi * f * n / fs);
% Compute its Discrete Fourier Series (DFS) using FFT
X = fft(x);
% Plot the finite-duration sinusoid and its DFS
figure;
subplot(2, 1, 1);
stem(n, x, 'filled', 'LineWidth', 1.5);
title('Finite-Duration Sinusoidal Signal');
xlabel('n (samples)');
ylabel('x[n]');
grid on;
subplot(2, 1, 2);
f_x = abs(X);
stem(0:N-1, f_x, 'filled', 'LineWidth', 1.5);
title('DFS Magnitude of Finite-Duration Sinusoid');
xlabel('Frequency Index (k)');
ylabel('|X[k]|');
grid on;
% Make the signal periodic by replicating it
x_periodic = repmat(x, 1, 2); % Replicate the signal
% Compute its DFS
X_periodic = fft(x_periodic);
% Plot the periodic signal and its DFS
figure;
subplot(2, 1, 1);
stem(0:length(x_periodic)-1, x_periodic, 'filled', 'LineWidth', 1.5);
title('Periodic Sinusoidal Signal (Replicated)');
xlabel('n (samples)');
ylabel('x[n]');
grid on;
subplot(2, 1, 2);
f_x_periodic = abs(X_periodic);
stem(0:length(x_periodic)-1, f_x_periodic, 'filled', 'LineWidth', 1.5);
title('DFS Magnitude of Periodic Sinusoid');
xlabel('Frequency Index (k)');
ylabel('|X[k]|');
grid on;
8. DTFT of x[n] = 0.5^n for n ≥ 0
% Define the signal parameters
N = 50; % Number of samples (considering up to n = 50)
n = 0:N-1; % n >= 0, thus n ranges from 0 to N-1
x = 0.5 * n; % Signal x[n] = 0.5n for n >= 0
% Define the frequency range for DTFT
omega = linspace(-pi, pi, 1024); % Frequency range from -pi to pi
% Compute the DTFT using the formula X(omega) = sum(x[n] * e^(-j*omega*n))
X_omega = zeros(size(omega)); % Initialize DTFT
for k = 1:length(omega)
X_omega(k) = sum(x .* exp(-1j * omega(k) * n)); % DTFT computation
end
% Plot the magnitude and phase of the DTFT
figure;
subplot(2,1,1);
plot(omega, abs(X_omega), 'b', 'LineWidth', 1.5);
title('Magnitude of DTFT of x[n] = 0.5n for n \geq 0');
xlabel('\omega (rad/sample)');
ylabel('|X(\omega)|');
grid on;
subplot(2,1,2);
plot(omega, angle(X_omega), 'r', 'LineWidth', 1.5);
title('Phase of DTFT of x[n] = 0.5n for n \geq 0');
xlabel('\omega (rad/sample)');
ylabel('Phase(X(\omega))');
grid on;
9. Signal Reconstruction using dtft and ideal interpolators
% Parameters for the original signal
N = 50; % Total number of samples
f = 5; % Frequency of the sinusoidal signal (in Hz)
fs = 32; % Sampling frequency (in Hz)
n = 0:N-1; % Sample indices
x_original = sin(2 * pi * f * n / fs); % Original sinusoidal signal
% Undersample the signal (take every 2nd sample, for example)
x_undersampled = x_original(1:2:end); % Undersample by a factor of 2
n_undersampled = 0:floor((N-1)/2); % Indices for the undersampled signal
% Reconstruct the signal using DTFT and Ideal Interpolators
omega = linspace(-pi, pi, 1024); % Frequency range for DTFT
X_undersampled = zeros(size(omega)); % Initialize DTFT for undersampled signal
% Compute the DTFT of the undersampled signal
for k = 1:length(omega)
X_undersampled(k) = sum(x_undersampled .* exp(-1j * omega(k) * n_undersampled));
end
% Reconstruct the signal by taking the inverse DTFT (Ideal Interpolation)
x_reconstructed = zeros(1, N); % Reconstructed signal
n_full = 0:N-1; % Full set of indices for the original signal
% Inverse DTFT to reconstruct the signal
for n_reconstructed = 1:N
x_reconstructed(n_reconstructed) = (1 / (2*pi)) * trapz(omega, X_undersampled .* exp(1j *
omega * (n_reconstructed-1)));
end
% Plot the original, undersampled, and reconstructed signals
figure;
subplot(3,1,1);
stem(n, x_original, 'filled', 'LineWidth', 1.5);
title('Original Signal');
xlabel('n (samples)');
ylabel('x[n]');
grid on;
subplot(3,1,2);
stem(n_undersampled, x_undersampled, 'filled', 'LineWidth', 1.5);
title('Undersampled Signal');
xlabel('n (samples)');
ylabel('x[n]');
grid on;
subplot(3,1,3);
plot(n_full, x_reconstructed, 'b', 'LineWidth', 1.5);
title('Reconstructed Signal (Ideal Interpolation)');
xlabel('n (samples)');
ylabel('x[n]');
grid on;
% Compare the original and reconstructed signals
error = norm(x_original - x_reconstructed) / norm(x_original);
disp(['Reconstruction error: ', num2str(error)]);