How do I define HANTS matlab parameters?
Show older comments
I am using HANTS MATLAB code for the NDVI time series. I have defined the parameters as follows;
function [yOut, amp, phi]=ApplyHants(y,nb,nf,fet,dod,HiLo,low,high,delta)
nb = 22 ; total number of images
nf = 3 frequency above zero frequency
fet = 5
dod = 5
HiLo = 'none'
low = -1.0 ; minimum value of NDVI
high = 1.0 ; maximum value of NDVI
delta = 0.1
but it is giving output as zero values.
Please suggest me how to define these above mentioned parameters for my NDVI time series of one year time period having 22 images.
I would appreciate your kind help.
Gauri
5 Comments
Manikanta Aditya
on 2 May 2024
If you’re getting zero values as output, it might be due to the fet and dod parameters. These parameters control the fit of the harmonic function to the time series data. If the fit error tolerance (fet) is too high or the degree of overdeterminedness (dod) is too low, the function might not fit the data well, resulting in zero values.
Here are some suggestions:
- Try reducing the fet value. A smaller fet means the function will try to fit the data more closely.
- Increase the dod value. A higher dod means the function will use more observations to fit the harmonic function, which might result in a better fit.
gauri
on 2 May 2024
Manikanta Aditya
on 2 May 2024
Edited: Manikanta Aditya
on 2 May 2024
nb = 22; % This should be set to the total number of images/observations in your time series, which is 22 in your case.
nf = 4; % This parameter determines the number of harmonic components used to fit the time series. For an annual time series with 22 images, a value of 3 or 4 is typically recommended.
fet = 1.5; % This controls how closely the model fits the data. A smaller value will lead to a tighter fit, but may also overfit the noise. Start with a value around 1 or 2 and adjust based on the results.
dod = 8; % This determines the number of observations used for each fitted value. A higher value will make the fit more robust to outliers but may also smooth out real variations. For an annual time series with 22 images, a value between 6 and 10 is generally recommended.
HiLo = 'both'; % This parameter determines whether the HANTS algorithm should force the fitted values to stay within the specified range. Since NDVI values are bounded between -1 and 1, you can set HiLo = 'both' to ensure that the fitted values remain within this range.
low = -1; % Set these to -1 and 1, respectively, to match the expected range of NDVI values.
high = 1;
delta = 0.1; % This parameter controls the amount of smoothing applied to the fitted values. A value of 0.1 is a reasonable starting point.
[yOut, amp, phi] = ApplyHants(y, nb, nf, fet, dod, HiLo, low, high, delta);
Try these parameters can check once.. Tried to explain in the code snippet.
If you still get zero values as output, it may be an issue with the input data (y) or the implementation of the HANTS algorithm. In that case, you may need to double-check your input data and the HANTS code for any potential issues.
gauri
on 3 May 2024
Answers (2)
Manikanta Aditya
on 3 May 2024
Edited: Manikanta Aditya
on 3 May 2024
Hi @gauri
Please consider these as my suggestations from my research and exploration and try to see if you can resolve the issue. Might not completely fix the issue.
From your description, it seems like the dimensions of your input data might not be matching the expected dimensions in the 'ApplyHants' function. The function expects the input data to be three-dimensional in the form [time, lat, lon], but your data seems to be in the form [bands, lines, samples].
You can try transposing your data to match the expected dimensions.
Here’s how you can do it:
% Transpose the dimensions
image = permute(image, [3 2 1]);
% Now pass the transposed image to the function
[yOut, amp, phi] = ApplyHants(image, nb, nf, fet, dod, HiLo, low, high, delta);
This will rearrange your data dimensions to match the expected [time, lat, lon] format. Please try this and let me know if it helps.
By using permute(image, [1, 3, 2]), you are rearranging the dimensions of the image array to [time, samples, lines], which should match the expected order [time, lat, lon] for the ApplyHants function.
After making this change, the ApplyHants function should work as expected, and you should get the desired output instead of zero values.
8 Comments
gauri
on 4 May 2024
Manikanta Aditya
on 4 May 2024
Okay, hope the issue gets fixed sooner.
gauri
on 4 May 2024
Manikanta Aditya
on 5 May 2024
The error "Not enough data points." is likely occurring because the HANTS algorithm is not able to find enough valid data points (i.e., non-outliers) to fit the model. This can happen when there are too many missing values or outliers in the input data.
To overcome this error, you may need to adjust the input parameters or preprocess your data to reduce the number of missing values or outliers.
- Check your input data
- Adjust the fet and dod parameters
- Preprocess your data
- Check the fill_val value
if nout > noutmax
if any(abs(y - fill_val) < eps)
% If all data points are equal to fill_val (missing/outlier value)
ready = true(size(y));
yr = fill_val; % Set output to fill_val
outliers(:) = fill_val; % Mark all as outliers
else
% Check if there are enough non-missing/non-outlier data points
valid_points = abs(y - fill_val) >= eps;
if sum(valid_points(:)) < min_valid_points
% Not enough valid data points
error('Not enough data points. Consider adjusting the parameters or preprocessing your data.');
else
% Proceed with the algorithm
ready = false(size(y));
end
end
else
ready = false(size(y));
end
Manikanta Aditya
on 5 May 2024
Edited: Manikanta Aditya
on 5 May 2024
The error you're encountering is related to the way MATLAB handles logical operations with arrays. In MATLAB, the && and || operators expect scalar inputs or arrays of the same size. However, the condition any(~ready) returns a single logical value (true or false), which is incompatible with the array ready in the expression any(~ready) && (nloop < nloopmax).
To fix this issue, you can replace the logical AND operation (&&) with the element-wise AND operation (&), and wrap the condition with the any function, like this:
while any(~ready & (nloop < nloopmax))
% ... rest of the loop code ...
end
This way, the element-wise AND operation (&) is performed between the negated ready array (~ready) and the condition nloop < nloopmax, and then the any function checks if any element in the resulting logical array is true.
Alternatively, you can restructure the condition using && and any separately, like this:
while (any(~ready)) && (nloop < nloopmax)
% ... rest of the loop code ...
end
In this case, the any(~ready) expression returns a single logical value, which can be used with the && operator along with the scalar condition nloop < nloopmax.
John
on 10 May 2024
The error "Arrays have incompatible sizes for this operation" is likely due to a mismatch in the dimensions of the arrays involved in the element-wise multiplication operation
mat .* (p .* y)
In the HANTS code, the line
mat = zeros(min(2*nf+1, ni), ni);
initializes a matrix mat with dimensions
(min(2*nf+1, ni), ni)
. However, the dimensions of the arrays p and y are not explicitly specified in the code snippet you provided. To resolve this issue, you need to ensure that the dimensions of mat , p , and y are compatible for element-wise operations.
Here are a few steps you can try: Check the dimensions of your input data y by using the size function:
[ni, ny, nx] = size(y);
Ensure that the dimensions of mat match the expected dimensions based on the input data y .
The first dimension of mat should be
min(2*nf+1, ni)
, and the second and third dimensions should match the dimensions of y . You can modify the initialization of mat as follows:
mat = zeros(min(2*nf+1, ni), ny, nx);
Initialize the array p with the same dimensions as y :
p = ones(ni, ny, nx);
Modify the line that creates the logical array bool_out to ensure that the dimensions match:
bool_out = (y < low) | (y > high);
Update the line that assigns values to p based on bool_out :
p(bool_out) = 0;
Update the line that calculates nout :
nout = sum(p == 0, 'all');
After making these changes, the dimensions of mat , p , and y should be compatible, and the element-wise multiplication operation
mat .* (p .* y)
should work correctly. If you still encounter issues, provide more information about the dimensions of your input data y and the specific error message you receive.
Categories
Find more on Logical 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!