Created
October 19, 2011 01:05
-
-
Save endolith/1297227 to your computer and use it in GitHub Desktop.
Perfect sinc interpolation in Matlab and Python
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
% From http://phaseportrait.blogspot.com/2008/06/sinc-interpolation-in-matlab.html | |
% Ideally "resamples" x vector from s to u by sinc interpolation | |
function y = sinc_interp(x,s,u) | |
% Interpolates x sampled sampled at "s" instants | |
% Output y is sampled at "u" instants ("u" for "upsampled") | |
% (EXPECTS x, s, and u to be ROW VECTORS!!) | |
% Find the period of the undersampled signal | |
T = s(2)-s(1); | |
% When generating this matrix, remember that "s" and "u" are | |
% passed as ROW vectors and "y" is expected to also be a ROW | |
% vector. If everything were column vectors, we'd do. | |
% | |
% sincM = repmat( u, 1, length(s) ) - repmat( s', length(u), 1 ); | |
% | |
% So that the matrix would be longer than it is wide. | |
% Here, we generate the transpose of that matrix. | |
sincM = repmat( u, length(s), 1 ) - repmat( s', 1, length(u) ); | |
% Equivalent to column vector math: | |
% y = sinc( sincM'/T )*x'; | |
y = x*sinc( sincM/T ); | |
end |
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
def sinc_interp(x, s, u): | |
""" | |
Interpolates x, sampled at "s" instants | |
Output y is sampled at "u" instants ("u" for "upsampled") | |
from Matlab: | |
http://phaseportrait.blogspot.com/2008/06/sinc-interpolation-in-matlab.html | |
""" | |
if len(x) != len(s): | |
raise Exception, 'x and s must be the same length' | |
# Find the period | |
T = s[1] - s[0] | |
sincM = tile(u, (len(s), 1)) - tile(s[:, newaxis], (1, len(u))) | |
y = dot(x, sinc(sincM/T)) | |
return y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks for the code! for simple speedup, based on this Gist see here - also with a FFT based implementation.