Is fftshift not working here ?
8 views (last 30 days)
Show older comments
I have a set of data from a thermocouple
I have this data in the form of a text file and importated it into matlab without issue
When trying to apply an fft() to this data set the output seemed fine
But when trying to apply an fftshift() it seemed to not work
in the sense that it produced a graph which looked identical to just the plotting of the voltage against time
This is the code showing how the shiftedFFT is identitical to the plotting of the voltage against time
I have a sneaking suspsion that im supposed to apply the fftshift to the data ive already applied the fft to but im not sure
clear,clearvars, close,clc
Scope_Data = load("Data from scope 2.txt");
Time_Data = Scope_Data(:,1);
Volatge_Data = Scope_Data(:,2);
Real_Data = real(fftshift(Volatge_Data,8192));
Img_Data = imag(fftshift(Volatge_Data,8192));
subplot(2,3,1)
plot(Real_Data)
xlabel("Frequency Index")
ylabel("Real Part of FFT of Signal")
title("Real_fftshift")
subplot(2,2,2)
plot(Img_Data)
xlabel("Frequency Index")
ylabel("Img Part of FFT of Signal")
title("Img_fftshift")
subplot(2,3,1)
plot(Real_Data)
xlabel("Frequency Index")
ylabel("Real Part of FFT of Signal")
title("Real_fftshift")
subplot(2,2,2)
plot(Img_Data)
xlabel("Frequency Index")
ylabel("Img Part of FFT of Signal")
title("Img_fftshift")
subplot(2,2,3)
plot(Volatge_Data)
1 Comment
David Goodmanson
on 22 Apr 2024
Hello Ziad
you have not applied the fft to the data. You need to do that first, so
fftshift(fft(voltage_data))
in which case fftshift, working now in the frequency domain, puts the data corresponding to zero frequency at the center of the array.
Answers (1)
prabhat kumar sharma
on 30 Apr 2024
Hi Ziad,
I understand that your shiftedFFT is identitical to the plotting of the voltage against time.
Here Your suspicion is correct; the fftshift() function should be applied to data that has already undergone the Fourier Transform via fft(), not directly on the time-domain signal (e.g., your voltage data). The purpose of fftshift() is to shift the zero-frequency component to the center of the spectrum, which is useful for visualization and analysis purposes, especially when dealing with symmetric spectra around the zero frequency.
Here is the below code for your refrence to correctly apply fft() followed by fftshift() to your data:
clear; clearvars; close all; clc;
% Load your data
Scope_Data = load("Data from scope 2.txt");
Time_Data = Scope_Data(:,1);
Voltage_Data = Scope_Data(:,2);
% Apply FFT on your voltage data
FFT_Data = fft(Voltage_Data);
% Now apply fftshift to the FFT data
Shifted_FFT_Data = fftshift(FFT_Data);
% Separating the real and imaginary parts
Real_Data = real(Shifted_FFT_Data);
Img_Data = imag(Shifted_FFT_Data);
% Frequency vector for plotting
Fs = 1 / mean(diff(Time_Data)); % Sampling frequency, assuming uniform sampling
N = length(Voltage_Data); % Number of points
f = (-N/2:N/2-1)*(Fs/N); % Frequency vector
% Plotting
subplot(3,1,1)
plot(f, Real_Data)
xlabel("Frequency (Hz)")
ylabel("Real Part of FFT of Signal")
title("Real Part of Shifted FFT")
subplot(3,1,2)
plot(f, Img_Data)
xlabel("Frequency (Hz)")
ylabel("Imaginary Part of FFT of Signal")
title("Imaginary Part of Shifted FFT")
subplot(3,1,3)
plot(Time_Data, Voltage_Data)
xlabel("Time (s)")
ylabel("Voltage (V)")
title("Original Voltage Signal")
This approach will give you a more accurate representation of how your signal behaves in the frequency domain, with the zero frequency component centered, making it easier to analyze the spectrum of your signal.
I hope it helps!
0 Comments
See Also
Categories
Find more on Spectral Measurements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!