Using Matlab ifft() to convert an exoprted frequency spectrum csv from other tool, how to get right amplitude ?

3 views (last 30 days)
Hi, folks:
I have a question, if you could kindly give some suggestions,
I have a csv-like file format exported from other tool, it stores the frequency-spectrum data using the tool's FFT feature of a signal in time domain , the file has the columns of 'freq' and value of complex 'real, imaginary'.
I can successfully read the file from the codes shows below, but I need to use Matlab to convert this to time domain after doing some freq-manipulation. But my problem met here is , I can't get the right amplitude when I at the first try to convert the FFT's data into time domain, it didn't seem as same as the original time data, the shape is 'OK', but I found the amplitude is not correct. Can you help me to correct it ?
I have attached the csv-like file, and the codes is below.
Thank you very much.
% FFT_to_time.m
filename1 = 'FFT.txt';
fileid = fopen(filename1,'r'); % open file for read
str_format = ['%f' char(9) '%f,%f'];
% tline = fgetl(fileid);
C = textscan(fileid, str_format);
fclose(fileid); % close the files id
freq_axis1 = C{1}; % frequency
mag_real = C{2}; % real part
mag_imaginary = C{3}; % imaginary part
C1_cmplx = mag_real+mag_imaginary*i;
mag_db1 = mag2db(abs(C1_cmplx));
plot(log10(freq_axis1),(mag_db1));
legend('filename_of_LTspice_FFT_exporteds Legend');
grid on;
title('FFT analysis');
xlabel('Freq 10^x (Hz)');
ylabel('Signal Mag (dB)');
% time domain, using ifft
n = size(C{1});
figure();
plot(1:1:n,ifft(C1_cmplx,'symmetric'))
grid on;

Answers (1)

Star Strider
Star Strider on 21 Mar 2022
The signal has an extremely high level of broadband noise, and the recovered time domain signal reflects that.
One option to deal with the broadband noise in the Fourier transform is to use the Savitzky-Golay filter (sgolayfilt) to smooth the Fourier transform (although ideally, this should have been done on the original time domain signal before doing the Fourier transform).
My result —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/935504/FFT.txt', 'VariableNamingRule','preserve')
T1 = 65536×2 table
Var1 Var2 ________________________________________________ _________ {'0.00000000000000e+000→-5.22940068994178e-002'} 0 {'2.22222222222222e+001→-1.08270387119659e-001'} 0.049634 {'4.44444444444444e+001→6.32497271425084e-002' } -0.044571 {'6.66666666666667e+001→-6.13301235606858e-003'} -0.062847 {'8.88888888888889e+001→-3.92422772081533e-002'} -0.024237 {'1.11111111111111e+002→-6.74400017368259e-002'} -0.31628 {'1.33333333333333e+002→7.33560829136178e-002' } 0.22552 {'1.55555555555556e+002→-1.56851504984966e-002'} 0.043187 {'1.77777777777778e+002→3.39599251255021e-003' } 0.056143 {'2.00000000000000e+002→7.27172126746549e-004' } 0.043386 {'2.22222222222222e+002→4.10926890393367e-003' } 0.032159 {'2.44444444444444e+002→8.17565718323461e-003' } 0.035392 {'2.66666666666667e+002→3.90379376573465e-003' } 0.018484 {'2.88888888888889e+002→7.67273422010580e-004' } 0.024847 {'3.11111111111111e+002→1.87448112857508e-003' } 0.022799 {'3.33333333333333e+002→3.31957092955135e-003' } 0.021849
T1s = arrayfun(@(x)strsplit(cell2mat(x),'\t'), T1{:,1}, 'Unif',0); % Split 'Var1' Into Two Separate Columns
T1s = cell2mat(cellfun(@str2double,T1s, 'Unif',0)); % Convert To Double Values
Freq = T1s(:,1); % Frequency Vector
Re = T1s(:,2); % Real Component Of The Fourier Transform
Im = T1.Var2; % Imaginary Component Of The Fourier Transform
FT1 = Re+1j*Im; % Complex Fourier Transform
Fn = Freq(end); % Nyquist Frequency
Fs = Fn*2; % Sampling Frequency
figure
subplot(2,1,1)
plot(Freq, mag2db(abs(FT1)))
grid
ylabel('Amplitude (dB)')
subplot(2,1,2)
plot(Freq, angle(FT1))
grid
xlabel('Frequency')
ylabel('Phase (rad)')
sgtitle('One-Sided Fourier Transform')
FT2 = [FT1; flipud(conj(FT1))]; % Create Symmetric Two-Sided Fourier Transform
IFT = ifft(FT2,'symmetric'); % Take The Inverse
t = linspace(0,numel(FT2-1),numel(FT2))/Fs; % Time Vector
figure
yyaxis left
plot(t, real(IFT))
ylabel('Real Amplitude')
yyaxis right
plot(t, imag(IFT))
grid
xlabel('Time')
ylabel('Imaginary Amplitude')
title('Recovered Time Domain Signal')
.

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!