What is the best way to inerapolate Nans in a time series.
4 views (last 30 days)
Show older comments
Hello,
I am plotting a map of the amplitude of the seasonal cycle in surface chlorophyll a concentrations in the North Atlantic. To do this I am using a nested for loop and am doing a fast Fourier transform for every point to calculate the amplitude in the annual cycle. Because the data was obtained remotely, there are some nans in the time series at some of the locations placed these NaN values with the mean surface chlorophyll a concentration, is there a more representative way to replace these NaN values? Is It possible to replace these NaNs by linear interpolation instead? Below is my nested for loop. for i = 1:size(chlo_amps,2) for j = 1:size(chlo_amps,3)
mean_chlo = nanmean(chlo_new);
chlo_new(isnan(chlo_new)) = mean_chlo; %Here I replace Nans with the mean
FCHLO_amps = fft(chlo_new,N); %Here I compute the fast fourier transform
FCHLOshift_amps = fftshift(FCHLO_amps);
FCHLOmag_amps = fftshift(abs(FCHLO_amps));
mag = sum(FCHLOmag_amps(f_chl > 0.99 & f_chl < 1.11)); %magnitude of the annual cycle
chlo_amp(i,j) = mag/N2_chl; %amplitude of the annual cycle
end
end
Any help would be greatly appreciated!
0 Comments
Answers (2)
Adam
on 7 Dec 2015
As an example, the following can interpolate data containing NaNs although obviously the more consecutive NaNs there are the more inaccurate it would get.
x = [1 2 3 NaN NaN 6 7 8 9 10];
y = x.^2;
yNew = interp1( x(~isnan(x) ), y(~isnan(x)), 1:10 )
or
yNew = interp1( x(~isnan(x) ), y(~isnan(x)), 1:10, 'pchip' )
or any of the other methods in the documentation of interp1 for slightly different interpolation functions.
2 Comments
Adam
on 7 Dec 2015
Yes, it would be just the same. I just gave this for a quick example. The idea really being that you have to take out the times corresponding to the NaNs when you give the input to the interp function. In my example it is clear really that the missing x values are just 4 and 5. It is obvious what the y values should be too just because I used an analytical function for ease of example, but really it would just be those y values missing and the x values would all be there, but be thrown away where there are NaNs in y
Chad Greene
on 7 Dec 2015
There are a number of ways to skin this cat. The first question I'd ask: Is a particular grid cell's value more affected by the values surrounding it in space, or is it more a function of the values in that grid cell before and after that particular time step? That is, do you want to interpolate through space or time?
If you want to interpolate through time you can use repnan. If you want to interpolate through space I recommend John D'Errico's inpaint_nans.
0 Comments
See Also
Categories
Find more on Descriptive Statistics 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!