-
Notifications
You must be signed in to change notification settings - Fork 0
/
ratresamp.m
64 lines (46 loc) · 1.66 KB
/
ratresamp.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
function output = ratresamp (signal, stretchFactor, squeezeFactor)
% -- ratresamp (S, L, M)
%
% Resamples a matrix S where each column represents one channel of a
% sampled signal.
%
% The signal will be stretched by a factor of L and squeezed by a
% factor of M. Thus the new sample rate will be L/M times the original
% sample rate. Both L and M must be integers greater than zero.
%
% The frequency spectrum of the signal will be filtered to prevent
% aliasing from occurring.
if (nargin != 3)
error([...
"Invalid call to ratresamp. Correct usage is:"...
"\n\n -- ratresamp (S, L, M)"...
]);
elseif (...
stretchFactor != round(stretchFactor) || ...
squeezeFactor != round(squeezeFactor) || ...
stretchFactor < 1 || squeezeFactor < 1 ...
)
error("L and M must be integers greater than zero.")
end
inputLength = length(signal);
outputLength = inputLength*stretchFactor/squeezeFactor;
% ## STRETCHMENT STAGE
% Inserts L - 1 zeros between each sample
stretchedInput((1:inputLength)*stretchFactor,:) = stretchFactor*signal(1:inputLength,:);
% ## FILTRATION STAGE
outputFft = fft(stretchedInput);
% Depending on which is greater, applies a low-pass filter...
if (stretchFactor > squeezeFactor);
%...to remove spectrum images...
passband = round(inputLength/2);
else
%...or to prevent aliasing.
passband = round(outputLength/2);
end
% This way, both spectrum and aliasing are removed with just one filter
outputFft(passband:end - passband,:) = 0;
stretchedInput = real(ifft(outputFft));
% ## SHRINKAGE PHASE
% Takes 1 out of M samples in the stretched signal
output(1:outputLength,:) = stretchedInput((1:outputLength)*squeezeFactor,:);
endfunction