-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResampleTolerant.m
41 lines (37 loc) · 1.18 KB
/
ResampleTolerant.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
function newvals = ResampleTolerant(vals,length1,length2)
% Wrapper around the resample function that allows it to work even if
% the product of the lengths are long enough to overwhelm the resample.m
% limit of 2^31
% Works by finding a rational number approximation of the requested length
% ratio... to within a particular tolerance.
% INPUTS
% vals = vector of values to be resampled
% length1 = desired length
% length2 = initial length (often equals length(vals))
%
% Dan Levenstein code made into a function by Brendon Watson
% August 2016
if length1*length2 < 2^31 % if no need to change factors don't
newvals = resample(vals,length1,length2);
else
newvals = [1 1];
resamplefact = length1/length2;
tol = 0.0001;
while length(newvals(:,1)) ~= length1
[P,Q] = rat(resamplefact,tol);
if P==0
tol = tol/10;
continue
end
if P*Q >=2^20 || tol<1e-300 %Avoid crashing resample...
vals([1,end],:) = [];
length2 = length2-2;
resamplefact = length1/length2;
tol = 0.0001;
continue
end
newvals = resample(vals,P,Q);
tol = tol/10;
end
end
end