-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created a rational resample MATLAB/Octave function
- Loading branch information
1 parent
53d01c8
commit 1e4a9cb
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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)(1); | ||
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 |