Skip to content

Instantly share code, notes, and snippets.

@mike-lawrence
Created January 22, 2014 19:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mike-lawrence/8565774 to your computer and use it in GitHub Desktop.
Save mike-lawrence/8565774 to your computer and use it in GitHub Desktop.
R code for complex demodulation of a time series. Applies complex demodulation to a timeseries twice, once on the signal then once on the reverse of the signal, re-reversing the latter and finally averaging the two to correct for artificial shift induced by the butterworth filter.
cd = function(s,freq,srate){
x = s*exp(2 * pi * 1i * freq * (1:length(s)) / srate )
zR1 = filter( x = Re(x) , filt = butter(3,freq/srate/2) )
zI1 = filter( x = Im(x) , filt = butter(3,freq/srate/2) )
z1 = 2*sqrt((zR1^2)+(zI1^2))
x = rev(x)
zR2 = filter( x = Re(x) , filt = butter(3,freq/srate/2) )
zI2 = filter( x = Im(x) , filt = butter(3,freq/srate/2) )
z2 = 2*sqrt((zR2^2)+(zI2^2))
z = (z1+rev(z2))/2
return(as.numeric(z))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment