FUNCTION LPassFilter,waveform,ThreedBpoint,sample_rate
;
; Digital single-pole low-pass filter of input waveform. Can be used to bandwidth limit a waveform.
; Function returns filtered waveform. Uses the convolution method of digital filtering in the time domain.
;------------------------------
; Simon Tulloch June 10 2013
; smt AT qucam dot com
;------------------------------
; waveform = a float array in units of volts
; ThreedBpoint= The required half-power point of the filter (i.e the frequency at which the voltage gain = 1/root2 )
; sample_rate=sample rate of data in waveform
;
; Build the filter response. Low pass filter is a weighted average of past values where weight=exp(-dT/RC)
; see page 24 Horowitz and Hill
; The phase shift at the 3dB point should be 45 degrees, the slope of the filter should be -6dB per decade.
;
; The input waveform needs to be padded at start with zeroes. Number of zeroes = length of filter response.
; Due to some oddity of the CONVOL function the output waveform has a phase shift = 1/2 of the length of filter response
; and this is corrected for.
;
RC=1/(2*!PI*ThreedBpoint) ; seconds
; Now translate to units of sample periods by multiplying by sample frequency
RC*=sample_rate
; We should go back 10 RC time constants to be sure of good accuracy
timeconstants=10.0
length=LONG(RC*timeconstants)
IF (length EQ 0) THEN RETURN,waveform
filter=FLTARR(length)
paddedwaveform=FLTARR(N_ELEMENTS(waveform)+length)
paddedwaveform[length:N_ELEMENTS(paddedwaveform)-1]=waveform
FOR c=0,length-1 DO BEGIN
filter[c]=EXP(-1.0*c/RC)
ENDFOR
; Mirror the filter response to make it causal
filter=REVERSE(filter)
;Normalise weights
filter=filter/TOTAL(filter)
filtered_signal=CONVOL(paddedwaveform,filter)
RETURN,filtered_signal[1+length/2:(1+length/2)+N_ELEMENTS(waveform)-1]
END