Azzera filtri
Azzera filtri

How to interpolate from a dataset using interp3?

48 visualizzazioni (ultimi 30 giorni)
Hi,
I have a dataset (4-D complex double) of dimensions 32 x 31 x 21 x 6. The first three parameters 32x31x21 defines two frequencies and heading values, and together theat creates a force variation in 3D space. The last parameter (6) refers to the degrees of freedom. Now, I want to interpolate this data set for a given two frequencies and a heading angle value and obtain the correct complex force value. I have attached the sample dataset here (sampleData.mat), and I'd suppose a sample code would look like the following, but it's not working.
I haven't worked much with interp functions, so I really appreciate your help.
clear; clc;
load('sampleData.mat');
freq1 = 0.01; freq2 = 0.01; heading = 0.01; % Sample values. Simply used for demonstration.
for i = 1:6
x(i,:) = interp3(X, Y, Z, dataset(:,:,:,i), freq1, freq2, heading);
end

Risposta accettata

Stephen23
Stephen23 il 29 Apr 2023
Modificato: Stephen23 il 29 Apr 2023
"How to interpolate from a dataset using interp3?"
It is very simple: don't use INTERP3.
"It's completely unlogical"
It is perfectly logical.
It is because users often prefer the array dimensions to correspond to what a matrix/array looks like: the vertical dimension (1) corresponding to the vertical axes (Y), the horizontal dimension (2) corresponding to the horizontal axes (X). But these are swapped around! Yes, they are. And that is exactly how image data are displayed. All image/plotting routines (not just MATLAB) use this interpretation.
Both INTERP2 and INTERP3 use this interpretation of the first two dimensions, because that is what users need.
You can find the TMW attempt at an explanation here:
and here:
Not only MATLAB has to handle this, all applications that process plotted/image data need to consider this. For example, Numpy names these two different interpretations as "ij" and "xy", see the "Notes" here:
Perhaps this could have been avoided if three hundred(?) years ago whoever defined the arbitrary order of matrix/array dimensions had instead specified them as being cols*rows*pages*... , which would probably also have avoided the endless column-major vs row-major flamewars. But too late, we have to work with what we have.
"What can be done to make it logical?"
The very simple solution is to use the correct tool.
For data in ND-grid format (regardless of how many dimensions) like yours you need to use INTERPN:
load('sampleData.mat');
freq1 = 0.01;
freq2 = 0.01;
heading = 0.01;
A = nan(6,1); % better to preallocate
for k = 1:6
A(k,1) = interpn(X, Y, Z, dataset(:,:,:,k), freq1, freq2, heading);
end
disp(A)
1.0e+07 * -0.0225 + 0.0010i 0.0049 - 0.0131i -0.0226 + 0.0077i -0.0763 - 0.0064i -3.2511 + 0.2187i -5.2563 + 0.0678i
Tip for the future: always read the documentation carefully, look at the related functions and topics linked to.
  1 Commento
Torsten
Torsten il 29 Apr 2023
I don't do image processing. And for me x corresponds to x, y corresponds to y and z corresponds to z.

Accedi per commentare.

Più risposte (1)

Torsten
Torsten il 28 Apr 2023
Modificato: Torsten il 28 Apr 2023
It's completely unlogical, but the documentation of interp3 says:
If X, Y, and Z are grid vectors, then size(V) = [length(Y) length(X) length(Z)]
where V corresponds to your 4d array "dataset".
So you will have to modify your code to:
clear; clc;
load('sampleData.mat');
freq1 = 0.01; freq2 = 0.01; heading = 0.01; % Sample values. Simply used for demonstration.
for i = 1:6
x(i,:) = interp3(X, Y, Z, permute(squeeze(dataset(:,:,:,i)),[2 1 3]), freq1, freq2, heading);
end
x
x =
1.0e+07 * -0.0225 + 0.0010i 0.0049 - 0.0131i -0.0226 + 0.0077i -0.0763 - 0.0064i -3.2511 + 0.2187i -5.2563 + 0.0678i
  1 Commento
Jake
Jake il 29 Apr 2023
Hi @Torsten! This does get the job done, thank you.
But is there a logical way to achieve this? I understand that a function is needed for interp3 to work, but here it's preset dataset. But I suppose what needs to be done is logical (trilinear interpolation of a pre computed dataset), however, the approach I've specified here is not. What can be done to make it logical?

Accedi per commentare.

Categorie

Scopri di più su Interpolating Gridded Data in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by