MATLAB Code
clc;
clear;
close all;
% =========================================================================
% === DEFINE A TIME INDEX 'n' CENTERED AROUND ZERO ===
% =========================================================================
N = 64; % Number of samples (keep it even for symmetry)
n = -N/2 : N/2-1; % MODIFIED: Time index now includes negative values
% --- Other Parameters ---
Fs = N; % Define Sampling Frequency in Hz
f0 = 8; % "Hardcore" frequency in Hz (e.g., 8 cycles in 1 sec)
omega0 = 2 * pi * f0 / Fs;% Convert Hz to rad/sample
% --- Frequency Axis for Plotting ---
f_norm = -0.5:0.001:0.5; % Normalized frequency
freq_hz = f_norm * Fs; % Frequency axis in Hz
w = 2 * pi * f_norm; % Angular frequency for DTFT calculation
% =========================================================================
% === 2. DEFINE SIGNALS USING THE NEW "TEXTBOOK" TIME INDEX 'n' ===
% =========================================================================
% MODIFIED: Signals are now defined logically based on the n vector.
delta = (n == 0); % Unit impulse at n=0
u = (n >= 0); % Unit step for n >= 0
complex_exp = exp(1j * omega0 * n); % Complex exponential (now non-causal)
a = 0.8;
real_exp = (a .^ n) .* u; % Real causal exponential a^n * u[n]
sin_wave = sin(omega0 * n); % Sine wave (now non-causal)
% Define a symmetric rectangular pulse of width 11 (-5 to 5)
pulse_width = 11;
rect_pulse = (abs(n) <= (pulse_width-1)/2);
% --- Store signals and names in a cell array ---
signals = {double(delta), double(u), complex_exp, real_exp, sin_wave, double(rect_pulse)};
names = {...
'Unit Impulse \delta[n]', ...
'Unit Step u[n]', ...
sprintf('Complex Exp e^{j 2\\pi (%d Hz) t}', f0), ...
'Causal Real Exp a^n u[n]', ...
sprintf('Sine Wave sin(16\\pi n)', f0), ... % CORRECTED: Label now matches variable
sprintf('Rectangular Pulse', pulse_width)};
% =========================================================================
% === 3. DTFT CALCULATION (No change needed here) ===
% =========================================================================
% This function correctly uses the 'n' vector from the workspace,
% so it works perfectly with our new n = [-32, ..., 31].
dtft = @(x, w_vec) arrayfun(@(omega) sum(x .* exp(-1j * omega * n)), w_vec);
% =========================================================================
% === 4. PLOT SIGNALS AND THEIR SPECTRUMS ===
% =========================================================================
for k = 1:length(signals)
% Calculate DTFT and normalize for consistent plotting
Xw = dtft(signals{k}, w);
max_val = max(abs(Xw));
if max_val > 0
Xw = Xw / max_val;
end
figure;
% --- Time Domain Plot (Now centered at n=0) ---
subplot(2,1,1);
stem(n, real(signals{k}), 'filled'); % MODIFIED: stem uses the new 'n' for the x-axis
title([names{k}]);
xlabel('Sample Index (n)');
ylabel('Amplitude');
grid on;
xlim([min(n) max(n)]); % Set limits to match the new 'n'
% --- Frequency Domain Plot (in Hz) ---
subplot(2,1,2);
plot(freq_hz, abs(Xw), 'LineWidth', 1.5);
title(['Magnitude Spectrum of ', names{k}]);
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;
xlim([-Fs/2 Fs/2]);
end
web('https://www.salimwireless.com/search?q=fourier%20transform', '-browser');