After using "FFT" getting NAN. Why?

clc
close all
clear all
%%
data = xlsread('freq15.xlsx','Group Name #2');
v= data(:,1);
plot (v);
title('Voltage signal with noise')
xlabel('Samples')
ylabel('Amplitudes')
%%
%plot magnitude specturm of a signal
clc
format long
X_mag=abs((fft(v)))
figure(1)
plot(X_mag)
xlabel('DFT Bins')
ylabel('Magnitude')
%%
Above is a code using FFT in MATLAB. The outcome of the code is NAN+NANi. How to get rid of NAN or what is the reason of this outcome?

2 件のコメント

Jan
Jan 2019 年 4 月 11 日
Please use the buttons to format the code. It is a good idea to post a question in the best readable format.
Omit the brute clearing header "clc; close all; clear all". This is a waste of time and cargo-cult-programming. If you want to keep your workspace clean, use functions.
Without knowing, what the contents of v is, there is no chance to understand, why abs((fft(v))) is NaN. So please show us, what your input data are.
Danish Ahmad
Danish Ahmad 2019 年 4 月 14 日
Thank you for your response.
Regards

サインインしてコメントする。

 採用された回答

Star Strider
Star Strider 2019 年 4 月 11 日

5 投票

... what is the reason of this outcome?
You probably either have a NaN in your data, or an empty cell. Both will appear as NaN in the xlsread output.
If that is the situation, your best option is to interpolate using the fillmissing (link) function (R2016b and later releases). The reason is that Fourier transforms require regularly-sampled time-domain signals for them to provide reliable output. Omitting the NaN value disrupts this regularity and results in an inaccurate fft result. Interpolating the NaN values will not provide as accurate a result has having the original value, however it is the only reliable alternative.

12 件のコメント

Danish Ahmad
Danish Ahmad 2019 年 4 月 14 日
It worked. Thank you for your response.
Regards
Star Strider
Star Strider 2019 年 4 月 14 日
As always, my pleasure.
Naveen P
Naveen P 2019 年 11 月 28 日
Simple, but effective reply. Solved my problem too.
Thansk for the question too
Star Strider
Star Strider 2019 年 11 月 28 日
@Naveen P — A vote would be appreciated!
Jan
Jan 2019 年 11 月 29 日
You got my vote at least. :-)
Star Strider
Star Strider 2019 年 11 月 29 日
@Jan — Thank you!
Rifat Afroz
Rifat Afroz 2020 年 2 月 26 日
@ Star Strider: is there any proper way to know how the NaNs should be filled depending on the nature of the dataset? I have a large frequency domain 2D matrix (403 x 11274) in which around half the data are NaNs and I need to ifft it. According to your explanation, I can't omit the NaNs to make the ifft operation go right. I tried interpolating the NaNs with fillmissing function and with the inpaint_nans inpaint_nans. But the ifft operation still seems incorrect. I was wondering if there is way to know how should the data look like to replace the NaNs in frequency domain.
Thank you.
Star Strider
Star Strider 2020 年 2 月 26 日
@Rifat Afroz — The problem with doing an ifft is that it is necessary to have the complete fft with all the positive and negative frequencies. (The ’negative’ frequencies only appear as such after using the fftshift function. The ifft must be done on the unshifted fft result.) The values in the positive and negative frequencies must appear in complex-conjugate pairs.
If you do not replace the NaN values with fillmissing, the entire ifft result will be NaN.
I had never considered the posssibility of using fillmissing to replace the NaN values in a complex vector such as an fft result. I did an experiment with that just now and found that fillmissing works very well (almost perfectly) and that with a relatively simple waveform, the fillmissing results were close to the original, including the sign of the imaginary part, so the ifft of the NaN-altered-and-filled fft was essentially the same as for the ifft of the original fft. (The NaN-altered fft was about 10% NaN values, so not trivial.)
In my ‘experiment’, I used the 'linear' method for the fillmissing interpolation. See if other methods — particularly 'spline' or 'pchip' — improve your results.
Rifat Afroz
Rifat Afroz 2020 年 2 月 27 日
Thanks for the tips. Still working on it.
Star Strider
Star Strider 2020 年 2 月 27 日
My pleasure.
Diego Soto Chávez
Diego Soto Chávez 2020 年 9 月 2 日
Hi Star Strider
know you another option like "fillmissing" but for previosly versiones of matlab? Thanks!!
Star Strider
Star Strider 2020 年 9 月 2 日
Something like this woulld likely work:
t = 1:8
y = rand(size(t))
y([3 5]) = NaN
ti = t;
ti(isnan(y)) = []
y(isnan(y)) = []
ynew = interp1(ti, y, t)
.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by