How to interpolate and smooth across values in a matrix, while ignoring NaN values?
32 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi there!
I am having some trouble figuring out how to interpolate across a matrix while ignoring NaN values. I have matrix data where each cell represents information from a single microphone, some of the microphones in the array malfunctioned, returning 0 values. I don't want these microphones contributing 0s to my interpolation, as the 0s don't represent real data, but rather nonfunctioning microphones. I set these malfunctioning microphones to NaN. My matrix is called 'scan' and looks something like this.
.3 .4 .3 .2 NaN .2
.4 .4 .3 .4 .2 .2
.3 NaN .5 .2 .4. .4
.3 .3 .4 .2 .2 .1
I need to interpolate across values in this matrix but I want the interpolation to ignore the NaN values, interpolating across them but without receiving them as input. Below is the interpolation code before I tried to have it ignore the NaN values
scan2 = scan'; %tranposes scan to scan2
interpImage = scatteredInterpolant(micsX(:),micsY(:), scan2(:), 'natural'); %interpolates scan2 onto fine grid, calls it enInterp
enInterp = interpImage(meshImageX, meshImageY);
K = 0.125*ones(1, 1); %smooths the interpolated data
enInterpSmooth = conv2(enInterp, K, 'valid');
enInterpSmooth = enInterpSmooth'; %puts enInterpSmooth back to the original orientation
The above code returns the interpolation with huge chunks of NaNs surrounding where the NaN microphones were.
I added the below code to try and get an interpolation that ignored the NaN values, but it still returned large amounts of NaNs.
% Replace NaN values in enInterpSmooth with interpolated values
nanIndices = isnan(enInterpSmooth);
enInterpSmooth(nanIndices) = enInterp(nanIndices); % Replace NaNs with interpolated values
Any recommendations for how to remedy this problem would be much appreciated!!
Thank y'all so much!
0 Kommentare
Akzeptierte Antwort
Voss
am 25 Apr. 2024
Avoid putting NaNs into the scatteredInterpolant:
idx = ~isnan(scan2);
interpImage = scatteredInterpolant(micsX(idx),micsY(idx), scan2(idx), 'natural'); %interpolates scan2 onto fine grid, calls it enInterp
enInterp = interpImage(meshImageX, meshImageY);
2 Kommentare
Weitere Antworten (1)
Mathieu NOE
am 26 Apr. 2024
hello
just for your information, if you have a recent matlab release you could use fillmissing2 function
scan = [0.3 0.4 0.3 0.2 NaN 0.2
0.4 0.4 0.3 0.4 0.2 0.2
0.3 NaN 0.5 0.2 0.4 0.4
0.3 0.3 0.4 0.2 0.2 0.1]
scan2 = fillmissing2(scan,"v4")
2 Kommentare
Mathieu NOE
am 26 Apr. 2024
you can also use inpaintn available on the fex
the result may slightly differ as we are not using the same approach
scan2 =
0.3000 0.4000 0.3000 0.2000 0.1599 0.2000
0.4000 0.4000 0.3000 0.4000 0.2000 0.2000
0.3000 0.3943 0.5000 0.2000 0.4000 0.4000
0.3000 0.3000 0.4000 0.2000 0.2000 0.1000
Mathieu NOE
am 26 Apr. 2024
I have one more suggestion (John would have been unhappy if I had forgotten about his work !!)
scan2 =
0.3000 0.4000 0.3000 0.2000 0.1167 0.2000
0.4000 0.4000 0.3000 0.4000 0.2000 0.2000
0.3000 0.4222 0.5000 0.2000 0.4000 0.4000
0.3000 0.3000 0.4000 0.2000 0.2000 0.1000
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!