Bypassing the sampling limits of resample function of p*q<2^31
5 views (last 30 days)
Show older comments
So I have 3 arrays which I want to resample into equal length. I am using the code
C1=resample(c1,target_length,length(c1));
C2=resample(c2,target_length,length(c2));
C3=resample(c3,target_length,length(c3));
where target_length is 70000. The matrix sizes of c1,c2, and c3 are 1x72901, 1x72392, and 1x723532. As expected it gives an error due to the "The product of the downsample factor Q and the upsample factor P must be less than 2^31." I cannot use interp1 as I need unique points too. Is there a way around this or to bypass this restriction for this problem?
6 Comments
Answers (1)
Abhas
on 31 Dec 2024
The issue arises because the "interp1" function in MATLAB requires the "x" array to have unique values, and your voltage data (the x input) may not satisfy this condition. To bypass the limitations of "interp1" or "resample", you can use a custom approach to handle duplicate or non-unique "x" values by following the below steps:
- Identify and remove duplicate "x" values by averaging the corresponding "y" (capacity) values. This step ensures "interp1" works correctly.
- After handling duplicates, use "interp1" to interpolate the data to the desired "target_length".
- If "c1", "c2", or "c3" has too large or too small lengths, adjust them accordingly to avoid errors during resampling.
Here's a sample MATLAB code to achieve the same:
target_length = 70000;
c1 = rand(1, 72901);
c2 = rand(1, 72392);
c3 = rand(1, 723532);
% Example voltage arrays (replace with actual voltage data)
V1 = linspace(min(c1), max(c1), length(c1));
V2 = linspace(min(c2), max(c2), length(c2));
V3 = linspace(min(c3), max(c3), length(c3));
% Resample each dataset
C1 = handle_duplicates_and_resample(V1, c1, target_length);
C2 = handle_duplicates_and_resample(V2, c2, target_length);
C3 = handle_duplicates_and_resample(V3, c3, target_length);
% Display results
disp(size(C1));
disp(size(C2));
disp(size(C3));
% Function to handle duplicates and resample
function resampled_data = handle_duplicates_and_resample(x, y, target_length)
% Ensure unique x values
[x_unique, ~, idx] = unique(x, 'stable'); % 'stable' keeps original order
y_unique = accumarray(idx, y, [], @mean); % Average y values for duplicates
% Resample using interp1
x_resampled = linspace(min(x_unique), max(x_unique), target_length);
resampled_data = interp1(x_unique, y_unique, x_resampled, 'linear');
end
You may refer to the below MathWorks documentation links to know more about the same:
- https://www.mathworks.com/help/matlab/ref/double.unique.html
- https://www.mathworks.com/help/matlab/ref/accumarray.html
- https://www.mathworks.com/help/matlab/ref/double.interp1.html
I hope this helps!
0 Comments
See Also
Categories
Find more on Multirate Signal Processing 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!