# FIR filter - remove padded zeros

I am using this class to filter EKG data.

My sampling rate is 167/s and the length of data is 1670 (10 seconds), when I apply the filter I get the correct results but looks like the number of samples i get back is bigger (I guess because of zero padding to become power of 2).

How can I get back only the samples I filtered? I tried to copy first 1670 samples from the filtered array but looks like it contains zeros as well.

Thanks

• Hi! What was the length of your FIR filter ? Commented Oct 17, 2017 at 12:54
• Sorry, but I am not really into DSP, I know the concepts and I am using the linked FIR filter library. I guess all I need to know if there is a way to send n samples to the FIR filter and retrieve back same number of samples (this is required for visualization where I need to display the same number of points on charts filtered/unfiltered). Commented Oct 17, 2017 at 15:45
• I just read this on Matlab: Zero-phase filtering helps preserve features in a filtered time waveform exactly where they occur in the unfiltered signal. To illustrate the use of filtfilt for zero-phase filtering, consider an electrocardiogram waveform. Commented Oct 19, 2017 at 9:49

if your input signal has 1670 non zero values, you should have at least 1670 nonzero output values. You should make sure that your input is fully nonzero. If so, there is a problem.

The code indicates that you are using a FIR filter. If your FIR filter length is M, there should be 1670 + M -1 output data points. I don’t use Java so I can’t comment on it. Some FIR filters are performed directly in the time domain, and others are done in the frequency domain. The zero padding is really an implementation issue. Zero padding up to a power of 2 is a related implementation issue with FFT codes. Neither is absolutely necessary. A library like FFTW can produce a fast transform where a power of 2 isn’t necessary.

The general reason for using FFT based filtering, is that they require fewer math operations, AFTER you precompute your sine/cosine tables. The advantage manifests if you need to do many FFT sequences. If you just want to filter a single file, the direct method is usually faster.

Another common assumption in DSP is that one is doing real time processing which constrains filtering to causal filters. If you just want to process a file, you can run a filter backwards in time, as well as forward. In MATLAB/Octave there is a routine called filtfilt that does this. It is also cleaver in that the extra samples are canceled out.

So yes, there is a way to filter 1630 samples in and get 1630 samples out, and the output data will line up with original, without offsets.

I don’t know if you can find filtfilt in Java.

• Thanks for the explanation, I am getting the idea, so maybe an IIR filter like butterworth or biquad would be better? I am interested in performance as well because I will be filtereing almost 25 channels in relatime (around 2000 samples per channel). Commented Oct 17, 2017 at 16:13
• I am not really looking to have some Java code here, only to have the right direction so I will take this as answer and if you guys have more info to add please do . Commented Oct 17, 2017 at 16:14

Given a linear phase FIR filter $h[n]$ of length $L$, if you use the conv or filter function to produce the output samples $y[n]$ for a given input $x[n]$, then there will be a constant group delay $N$ associated with the type of FIR filter being used. So this delay of $N$ samples must be taken into account when evaluating the output result in applications where data phase is critical such as in the case of using a Hilbert transform to produce an analytic signal...

Assuming that you have a filter with odd length $L$ which can be expressed as $$L = 2 N + 1$$ then the associated group delay will be the integer $N$; i.e., the information in the output data will be shifted $N$ sample to the right, hence you may select the output after the Nth sample as such: $$y_i[n] = y_d[n-N]$$.

In a computer environment such as MATLAB/Octave this translates into matrix indices such that:

xi = yd(N+1:end-N)


where $y_d$ was the matrix that holds the result of the raw convolution output;i.e.,

yd = conv(x,h)


if you have used filter function to produce the raw data than the slicing should be done as:

xi = yd(N+1:end)


where $yd$ now is the matrix that holds the result of the raw filter output;i.e.,

yd = filter(h,1,x)


When the filter length $L$ is an even number, there happens a half-sample delay of information associated with the raw-data output of the computation and you should consider that as well. Therefore It's better to use an odd length FIR filter unless you have convincing reasons to do the otherwise.