## FIR filter with output same length as input which is also time-aligned with input

### FIR filter with output same length as input which is also time-aligned with input

I want to do some frame-based processing of an input signal with a
C-code program on a hardware DSP. One of the things I really want is
an FIR filter which gives an output signal with the same length (in
terms of samples) as the input signal and is also time-aligned with the
input (i.e no delay or offset). All of the FIR implementations I have
seen either result in a shorter output, delay the output by m/2 samples
(where m is the filter order), or truncate the last m/2 samples.

Since we are talking about frame-based processing and not
point-by-point processing, I think what I want should be possible. I
expect that the filter will probably need to be acausal, but I only
have experience with designing/implementing causal filters.

Does anyone hvae any code that implements this sort of filter or can
someone point me to a book, article, or webpage which describes how to
do it in sufficient detail that I would be able to implement it myself?

The MATLAB Signal Processing Toolbox command "filtfilt" comes very
close to doing what I want. It actually does something significantly
more complicated than what I need (and is thus significantly less
efficient than I would like). But it's existence is essentially proof
to me that what I want should be doable.

Any advice would be greatly appreciated.

### FIR filter with output same length as input which is also time-aligned with input

If your frames are contiguous samples from a stream of data, then
you should really save state between frames, or you will get chirps at
frame boundaries. Find a way to live with the m/2 delay.

If the data frames are standalone sequences that do not represent

With a symmetric FIR filter of length 2L+1, the group delay is L.
Convolving that with a vector of length K would yield a full convolution
sequence of length K+2L.

Assuming you are using a Matlab-style filter that gives you all of the
input-on transient and none of the input-off transient; Drop L samples
from the input-on transient and pad L zeros on the end of your input
sequence.

If all the above is true, this should nicely superimpose the input &
output vectors. The beginning and ending of the sequence will show some
warbling, but *if* you don't know what the signal was before or after,
then this is about as good as you can do.

I repeat. If the frames are chunks of a continuous signal -- keep the
state and find a way to deal with the group delay.

-- Mark Borgerding

### FIR filter with output same length as input which is also time-aligned with input

Alternatively, if you find a non-causal filter,
please post the code! :=)

Regards,
John

### FIR filter with output same length as input which is also time-aligned with input

Just chop off what you don't like. :)

dbell

### FIR filter with output same length as input which is also time-aligned with input

Mark,

The frames are contiguous samples from a sensor. However, I plan on
extracting relevant info/parameters on a frame-by-frame basis, at least
to start with, so I'm not overly concerned with discontinuities at
frame boundaries.

I quickly tested your advice in MATLAB regarding taking the full-blown
convolution and chopping m/2 samples off both the beginning and end of
the output. This seems to work very well except at the edges. I might
be able to live with that and I also think I might be able to make it
work a litlte better if I make some intelligent assumptions regarding
the filter's initial state. But your point about keeping the state is
well taken. It would obviously work much better if I saved some
information from the previous frame.

Todd

### FIR filter with output same length as input which is also time-aligned with input

John,

Implementing a non-causal filter is not an oxymoron if you are doing
frame-based processing (as opposed to point-by-point "hard" realtime
processing). Here is my pseudocode for implementing a noncausal
filter:

x = input signal
X = FFT{x}
A = desired magnitude response of filter
(could have A = FFT{h}, where h are filter coefficients)
y = output signal

y = IFFT{X * A}

So I found one and there's the code .... ;-)

I guess I was just wondering if there was an easy way to implement this
type of filtering in the time-domain and if so what the
question.

-Todd

### FIR filter with output same length as input which is also time-aligned with input

Todd,
I could not resist that comment and I am sure it did not cause any offence.

I think we differ as to what we mean by acausal.
I take an 'acausal' filter to be one in which the output is derived in
part at least from signal samples that have yet to arrive. The term
comes up in textbooks because it is easiest to define t=0 as the centre
of the impulse response. If I had an acausal filter (a very long one) I
think I could make a lot of money on the stock market, hence my request :=)

Unfortunately I don't think your pseudo-code qualifies because it does
not even begin processing until all the samples in one batch have arrived.

By the way, I don't think it makes any sense to talk about causality or
non-causality in regard to processing that is not real-time, but of
course this may depend on your definition.

Regards,
John

### FIR filter with output same length as input which is also time-aligned with input

...

John,

I have seen "acausal" loosely applied to off-line processing when the
center of the (usually symmetric) impulse is assigned the value t=0.
This simplifies the mathematics in many instances, but it has the
unfortunate effect of allowing the unwary to believe that latency has
been eliminated. It is easy to see how its formal definition (output
before input) and its metaphoric use (t=0 later than the first element)
can together create confusion.

Jerry
--
Engineering is the art of making what you want from things you can get.
?

### FIR filter with output same length as input which is also time-aligned with input

Jerry,
I thought I could indulge in a little pedantry, especially since Mark
Actually something useful did occur to me, and I hope Todd is still
following this thread. Todd, why not tack extra samples onto each
frame, taking these from the end of the previous frame and the start of
the next frame? If the impulse response is N samples long and you add
N/2 signal samples to each end then this will eliminate the 'end
effect,' as far as the original frame is concerned.

I am of course describing what is done with the 'overlap/save' method of
doing fast convolution, and this technique is well documented.

Regards,
John

### FIR filter with output same length as input which is also time-aligned with input

I don't see that you were any more pedantic than I was. I merely wanted
to enter a caveat; that the formal meaning of "causal" and the
colloquial way it is used to to mean a shifted time reference can lead
the unwary to believe that latency can be circumvented. I don't think I
contradicted you in any way.

Jerry
--
Engineering is the art of making what you want from things you can get.
?

### FIR filter with output same length as input which is also time-aligned with input

(snip, and much previously snipped)

What I probably would do is try to smooth the transition on each end
before filtering. For the first and last L points, multiply them by
some function that goes smoothly from 0 at the boundary to 1 L away from
the boundary.

If you are doing mostly low pass filtering it might not be so bad, but
if you do high pass filtering the boundary glitch can get amplified.

-- glen

### FIR filter with output same length as input which is also time-aligned with input

Jerry,
I understood the point you were making, and agree that it is easy for
some of the textbook terminology to intrude into the real world where it
is not always strictly applicalble.

Regards,
John