,Understand DTFT and DFT using MATLAB

6 次查看(过去 30 天)
I wish to understand the DTFT and DFT using MATLAB.
So I want to write a code which does the following:
  1. plot X1[n]=COS(2*pi*201/1024*n) where n=0 to1023
  2. plot X2[n]=rect(N) where N=1024 i.e. X2[n]=1 for n=0 to 1023; else X2[n]=0
  3. plot multiplication of both i.e. X1(n)*X2(n)
  4. plot magnitude plot of Y1(w)=DTFT of X1(n) for -pi<w<pi
  5. plot magnitude plot of Y2(w)=DTFT of X2(n) for -pi<w<pi
  6. plot convolution of Y1 and Y2 i.e. Z1(w)=Y1(w) ⊗ Y2(w)
  7. plot frequency samples Z1[k] that are values of Z1(w) at w[k]=2*pi*k/N where 0<k<(N-1)
Plz help me with the code
  2 个评论
KALYAN ACHARJYA
KALYAN ACHARJYA 2025-1-7
编辑:KALYAN ACHARJYA 2025-1-14
It's always more appreciated if you have specific questions or share what you have tried so far. There are so many literature available regarding the DTFT and DFT. If you have any specific questions, feel free to ask.
Pankaj Jha
Pankaj Jha 2025-1-23
移动:Walter Roberson 2025-1-23
Actually I am trying to understand and emulate the theory of spectral leakage when the signal tone does not fall in a freq. bin.
The flow that I want to emulate in MATLAB is as given below

请先登录,再进行评论。

回答(1 个)

Korosh Agha Mohammad Ghasemi
移动:Image Analyst 2025-1-10
clc;
clear;
close all;
%% Define parameters
N = 1024; % Number of samples
n = 0:N-1; % Time indices
w = linspace(-pi, pi, N); % Frequency range for DTFT
k = 0:N-1; % Frequency indices for DFT
%% Generate X1[n]
f1 = 201/1024; % Frequency of cosine in cycles/sample
X1 = cos(2 * pi * f1 * n);
%% Generate X2[n] (rectangular window)
X2 = ones(1, N); % Rectangular window of length N
%% Multiply X1[n] and X2[n]
X = X1 .* X2;
%% Compute DTFT of X1[n] and X2[n]
Y1 = fftshift(fft(X1, N)); % DTFT of X1[n] using zero-padding
Y2 = fftshift(fft(X2, N)); % DTFT of X2[n] using zero-padding
%% Convolution in the frequency domain
Z1 = conv(Y1, Y2, 'same');
%% Frequency samples (DFT values of Z1(w))
Z1_samples = Z1(mod(k, N) + 1); % DFT sampled points of Z1(w)
%% Plotting
figure;
% Plot X1[n]
subplot(4, 2, 1);
stem(n, X1, 'b', 'MarkerFaceColor', 'b');
title('X1[n] = cos(2\pi(201/1024)n)');
xlabel('n'); ylabel('Amplitude'); grid on;
% Plot X2[n]
subplot(4, 2, 2);
stem(n, X2, 'r', 'MarkerFaceColor', 'r');
title('X2[n] = rect(N)');
xlabel('n'); ylabel('Amplitude'); grid on;
% Plot X1[n] * X2[n]
subplot(4, 2, 3);
stem(n, X, 'g', 'MarkerFaceColor', 'g');
title('X[n] = X1[n] \times X2[n]');
xlabel('n'); ylabel('Amplitude'); grid on;
% Plot magnitude of DTFT of X1[n]
subplot(4, 2, 4);
plot(w, abs(Y1), 'b');
title('|Y1(w)|: DTFT of X1[n]');
xlabel('Frequency (rad/sample)'); ylabel('Magnitude'); grid on;
% Plot magnitude of DTFT of X2[n]
subplot(4, 2, 5);
plot(w, abs(Y2), 'r');
title('|Y2(w)|: DTFT of X2[n]');
xlabel('Frequency (rad/sample)'); ylabel('Magnitude'); grid on;
% Plot magnitude of convolution Z1(w)
subplot(4, 2, 6);
plot(w, abs(Z1), 'g');
title('|Z1(w)|: Convolution of Y1(w) and Y2(w)');
xlabel('Frequency (rad/sample)'); ylabel('Magnitude'); grid on;
% Plot frequency samples Z1[k]
subplot(4, 2, 7);
stem(k, abs(Z1_samples), 'k', 'MarkerFaceColor', 'k');
title('Z1[k]: Frequency samples');
xlabel('k'); ylabel('Magnitude'); grid on;

产品


版本

R2014b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by