How to plot simple fft of a shifted time series?

2 views (last 30 days)
Kelsey
Kelsey on 23 Apr 2014
Answered: Pratik on 26 Jun 2024
I am trying to understand basic fft functions of a shifted time series and my graphs are not appearing as I expect them to:
dt = 0.01; % time interval N_ts = 256; % number of time data Td_ts = 2.*(N_ts-1)*dt; % duration t_ts = 0:dt:Td_ts; % define time axis x_ts = zeros(1, length(t_ts)); %Initialize x_ts2 = zeros(1, length(t_ts)); %Initialize t0 = 100; t1 = 156; x_ts(t0)= 1/dt; % time shift of x(t+t0) x_ts2(t1) = 1/dt; % time shift of x(t-t0)
nfft_ts = 511; % Length of FFT X2_ts = fft(x_ts2,nfft_ts)*dt; % Take fft, padding with zeros so that Length(x) is equal to nfft
amp2 = abs(X2_ts); % Take the magnitude of fft of x phase2 = angle(X2_ts); % Take angle
figure(5); plot(t_ts,X2_ts); title('Built-In FFT'); xlabel('Time (s)'); ylabel('X(t)');

Answers (1)

Pratik
Pratik on 26 Jun 2024
Hi Kelsey,
To plot the FFT of a shifted time series and understand its behavior, it is important to ensure that the time and frequency domains are correctly handled. The current code has a few issues that need to be addressed:
  1. FFT Calculation and Plotting: The magnitude of the FFT should be plotted in the frequency domain rather than attempting to plot the FFT result directly against the time axis.
  2. Frequency Axis: The correct frequency axis for plotting the FFT result needs to be constructed.
  3. Time Shifting: Ensure that the time shifts are correctly applied and interpreted.
Here is the corrected and complete code for plotting the FFT of a shifted time series:
dt = 0.01; % time interval
N_ts = 256; % number of time data
Td_ts = (N_ts-1)*dt; % duration
t_ts = 0:dt:Td_ts; % define time axis
x_ts = zeros(1, length(t_ts)); % Initialize x_ts
x_ts2 = zeros(1, length(t_ts)); % Initialize x_ts2
t0 = 100; % Index for time shift of x(t+t0)
t1 = 156; % Index for time shift of x(t-t0)
x_ts(t0) = 1/dt; % time shift of x(t+t0)
x_ts2(t1) = 1/dt; % time shift of x(t-t0)
nfft_ts = 511; % Length of FFT
X2_ts = fft(x_ts2, nfft_ts) * dt; % Take fft, padding with zeros so that Length(x) is equal to nfft
amp2 = abs(X2_ts); % Take the magnitude of fft of x
phase2 = angle(X2_ts); % Take angle
% Frequency axis
f_ts = (0:nfft_ts-1)/(nfft_ts*dt); % Frequency axis in Hz
% Plotting the magnitude of the FFT
figure;
plot(f_ts, amp2, 'LineWidth', 1.5);
title('Magnitude of FFT of Shifted Time Series');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;
% Plotting the phase of the FFT
figure;
plot(f_ts, phase2, 'LineWidth', 1.5);
title('Phase of FFT of Shifted Time Series');
xlabel('Frequency (Hz)');
ylabel('Phase (radians)');
grid on;
I hope this helps!

Community Treasure Hunt

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

Start Hunting!