Main Content

dsp.FrequencyDomainFIRFilter

Filter input signal in frequency domain

Description

Thedsp.FrequencyDomainFIRFilterSystem object™ implements frequency-domain, fast Fourier transform (FFT)-based filtering to filter a streaming input signal. In the time domain, the filtering operation involves a convolution between the input and the impulse response of the finite impulse response (FIR) filter. In the frequency domain, the filtering operation involves the multiplication of the Fourier transform of the input and the Fourier transform of the impulse response. The frequency-domain filtering is efficient when the impulse response is very long. You can specify the filter coefficients directly in the frequency domain by settingNumeratorDomainto'Frequency'.

This object uses the overlap-save and overlap-add methods to perform the frequency-domain filtering. For filters with a long impulse response length, the latency inherent to these two methods can be significant. To mitigate this latency, thedsp.FrequencyDomainFIRFilterobject partitions the impulse response into shorter blocks and implements the overlap-save and overlap-add methods on these shorter blocks. To partition the impulse response, set thePartitionForReducedLatencyproperty totrue. For more details on these two methods and on reducing latency through impulse response partitioning, seeAlgorithms.

To filter the input signal in the frequency domain:

  1. Create thedsp.FrequencyDomainFIRFilterobject and set its properties.

  2. Call the object with arguments, as if it were a function.

To learn more about how System objects work, seeWhat Are System Objects?

Creation

Description

fdf= dsp.FrequencyDomainFIRFiltercreates a frequency domain FIR filter System object that filters each channel of the input signal independently over time in the frequency domain using the overlap-save or overlap-add method.

example

fdf= dsp.FrequencyDomainFIRFilter(num)creates a frequency domain FIR filter object with theNumeratorproperty set tonum.

Example:dsp.FrequencyDomainFIRFilter(fir1(400,2 * 2000 / 8000));

example

fdf= dsp.FrequencyDomainFIRFilter(Name,Value)creates a frequency domain FIR filter System object with each specified property set to the specified value. Enclose each property name in single quotes. You can use this syntax with any previous input argument combinations.

Example:dsp.FrequencyDomainFIRFilter('Method','Overlap-add');

Properties

expand all

Unless otherwise indicated, properties arenontunable, which means you cannot change their values after calling the object. Objects lock when you call them, and thereleasefunction unlocks them.

If a property istunable, you can change its value at any time.

For more information on changing property values, seeSystem Design in MATLAB Using System Objects.

Frequency-domain filter method, specified as either'Overlap-save'or'Overlap-add'. For more details on these two methods, seeAlgorithms.

Domain of the filter coefficients, specified as one of the following:

  • 'Time'–– Specify the time-domain filter numerator in theNumeratorproperty.

  • 'Frequency'–– Specify the filter's frequency response in theFrequencyResponseproperty.

FIR filter coefficients, specified as a row vector.

Tunable:Yes

Dependencies

This property applies whenNumeratorDomainis set to'Time'.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64
Complex Number Support:Yes

Frequency response of the filter, specified as a row vector or a matrix. WhenPartitionForReducedLatencyisfalse,FrequencyResponsemust be a row vector. The FFT length is equal to the length of theFrequencyResponsevector. WhenPartitionForReducedLatencyistrue,FrequencyResponsemust be a 2P-by-Nmatrix, wherePis the partition size, andNis the number of partitions.

Tunable:Yes

Dependencies

This property applies whenNumeratorDomainis set to'Frequency'.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64
Complex Number Support:Yes

Time-domain numerator length, specified as a positive integer-valued scalar.

Dependencies

This property applies whenNumeratorDomainis set to'Frequency'andPartitionForReducedLatencyis set tofalse.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64

Flag to specify if the filter coefficients are all real, specified astrueorfalse.

Dependencies

This property applies whenNumeratorDomainis set to'Frequency'.

Flag to partition numerator to reduce latency, specified as one of the following:

  • false–– The filter uses the traditional overlap-save or overlap-add method. The latency in this case isFFTLength– length(Numerator) + 1。

  • true——在这种模式下,对象分区numerator into segments of length specified by thePartitionLengthproperty. The filter performs overlap-save or overlap-add on each partition, and combines the partial results to form the overall output. The latency is now reduced to the partition length.

FFT长度指定为一个积极的我nteger. The default value of this property,[], indicates that the FFT length is equal to twice the numerator length. The FFT length must be greater than or equal to the numerator length.

Example:64

Dependencies

This property applies when you setNumeratorDomainproperty to'Time'andPartitionForReducedLatencyproperty tofalse.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64

Numerator partition length, specified as a positive integer less than or equal to the length of the numerator.

Example:40

Example:60

Dependencies

This property applies when you set theNumeratorDomainproperty to'Time'andPartitionForReducedLatencyproperty totrue.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64

This property is read-only.

Filter latency in samples, returned as an integer greater than 0. WhenPartitionForReducedLatencyisfalse, the latency is equal toFFTLength– length(Numerator) + 1。WhenPartitionForReducedLatencyistrue, the latency is equal to the partition length.

Data Types:uint32

Usage

Description

example

fdfOut= fdf(input)filters the input signal and outputs the filtered signal. The object filters each channel of the input signal independently over time in the frequency domain.

Input Arguments

expand all

Data input, specified as a vector or a matrix. This object supports variable-size input signals. That is, you can change the input frame size (number of rows) even after calling the algorithm. However, the number of channels (number of columns) must remain constant.

Example:randn(164,4)

Data Types:single|double
Complex Number Support:Yes

Output Arguments

expand all

Filtered output, returned as a vector or matrix. The size, data type, and complexity of the output match those of the input.

Data Types:single|double
Complex Number Support:Yes

Object Functions

To use an object function, specify the System object as the first input argument. For example, to release system resources of a System object namedobj, use this syntax:

release(obj)

expand all

fvtool Visualize frequency response of DSP filters
step RunSystem objectalgorithm
release Release resources and allow changes toSystem objectproperty values and input characteristics
reset Reset internal states ofSystem object

Examples

collapse all

Filter input signal using overlap-add and overlap-save methods, and compare the outputs to the output of a FIR filter.

Initialization

Design the FIR lowpass filter coefficients using thefir1function. The sampling frequency is 8 kHz, and the cutoff frequency of the filter is 2 kHz. The impulse response has a length of 400.

impL = 400; Fs = 8000; Fcutoff = 2000; imp = fir1(impL,2*Fcutoff/Fs);

Create twodsp.FrequencyDomainFIRFilterobjects and adsp.FIRFilterobject. Set the numerator of all the three filters toimp. Delay the FIR output by the latency of the frequency-domain filter.

fdfOA = dsp.FrequencyDomainFIRFilter(imp,'Method','overlap-add'); fdfOS = dsp.FrequencyDomainFIRFilter(imp,'Method','overlap-save'); fir = dsp.FIRFilter('Numerator',imp); dly = dsp.Delay('Length',fdfOA.Latency);

Create twodsp.SineWaveobjects. The sine waves generated have a sample rate of 8000 Hz, frame size of 256, and frequencies of 100 Hz and 3 kHz, respectively. Create atimescopeobject to view the filtered outputs.

frameLen = 256; sin_100Hz = dsp.SineWave('Frequency', 100,'SampleRate',Fs,...'SamplesPerFrame',frameLen); sin_3KHz = dsp.SineWave('Frequency',3e3,'SampleRate',Fs,...'SamplesPerFrame',frameLen); ts = timescope('TimeSpanOverrunAction','Scroll',...'ShowGrid',true,'TimeSpanSource','Property','TimeSpan',5 * frameLen/Fs,...'YLimits',[-1.1 1.1],...'ShowLegend',true,...'SampleRate',Fs,...'ChannelNames',{'Overlap-add','Overlap-save','Direct-form FIR'});

Streaming

Stream 1e4 frames of noisy input data. Pass this data through the frequency domain filters and the FIR filter. View the filtered outputs in the time scope.

numFrames = 1e4;foridx = 1:numFrames x = sin_100Hz() + sin_3KHz() + 0.01*randn(frameLen,1); yOA = fdfOA(x); yOS = fdfOS(x); yFIR = fir(dly(x)); ts([yOA,yOS,yFIR]);end

The outputs of all the three filters match exactly.

Partition the impulse response length of a frequency domain FIR filter. Compare the outputs of the partitioned filter and the original filter.

Design the FIR lowpass filter coefficients using thefir1function. The sampling frequency is 8 kHz and the cutoff frequency of the filter is 2 kHz. The impulse response is of length 4000.

impL = 4000; Fs = 8000; Fcutoff = 2000; imp = fir1(impL,2 * Fcutoff / Fs);

Create adsp.FrequencyDomainFIRFilterwith coefficients set to theimpvector. The latency of this filter is given by$FFT Length - Length(Numerator) + 1$, which is equal to 4002. By default, FFT length is equal to twice the numerator length. This makes the latency proportional to the impulse response length.

fdfOS = dsp.FrequencyDomainFIRFilter(imp,'Method','overlap-save'); fprintf('Frequency domain filter latency is %d samples\n',fdfOS.Latency);
频域滤波延迟是4002 samples

Partition the impulse response to blocks of length 256. The latency after partitioning is proportional to the block length.

fdfRL = dsp.FrequencyDomainFIRFilter(imp,'Method','overlap-save',...'PartitionForReducedLatency',true,...'PartitionLength',256); fprintf('Frequency domain filter latency is %d samples\n',fdfRL.Latency);
Frequency domain filter latency is 256 samples

Compare the outputs of the two frequency domain filters. The latency offdfOSis 4002, and the latency offdfRLis 256. To compare the two outputs, delay the input tofdfRLby 4002 - 256 samples.

dly = dsp.Delay('Length',(fdfOS.Latency-fdfRL.Latency));

Create twodsp.SineWaveobjects. The sine waves have a sample rate of 8000 Hz, frame size of 256, and frequencies of 100 Hz and 3 kHz, respectively. Create atimescopeobject to view the filtered outputs.

frameLen = 256; sin_100Hz = dsp.SineWave('Frequency', 100,'SampleRate',Fs,...'SamplesPerFrame',frameLen); sin_3KHz = dsp.SineWave('Frequency',3e3,'SampleRate',Fs,...'SamplesPerFrame',frameLen); ts = timescope('TimeSpanOverrunAction','Scroll','ShowGrid',true,...'TimeSpanSource','Property','TimeSpan',5 * frameLen/Fs,...'YLimits',[-1.1 1.1],...'ShowLegend',true,...'SampleRate',Fs,...'ChannelNames',{'Overlap-save With Partition','Overlap-save Without Partition'});

Stream 1e4 frames of noisy input data. Pass this data through the two frequency domain filters. View the filtered outputs in the time scope.

numFrames = 1e4;foridx = 1:numFrames x = sin_100Hz() + sin_3KHz() + .1 * randn(frameLen,1); yRL = fdfRL(dly(x)); yOS = fdfOS(x); ts([yRL,yOS]);end

The outputs match exactly.

Specify the numerator coefficients of the frequency-domain FIR filter in the frequency domain. Filter the input signal using the overlap-add method. Compare the frequency-domain FIR filter output to the corresponding time-domain FIR filter output.

Initialization

Design the FIR lowpass filter coefficients using thefir1function. The sampling frequency is 8 kHz, and the cutoff frequency of the filter is 2 kHz. The time-domain impulse response has a length of 400. Compute the FFT of this impulse response and specify this response as the frequency response of the frequency-domain FIR filter. Set the time-domain numerator length, specified by theNumeratorLengthproperty, to the number of elements in the time-domain impulse response.

impL = 400; Fs = 8000; Fcutoff = 2000; imp = fir1(impL,2 * Fcutoff / Fs); H = fft(imp , 2 * numel(imp)); oa = dsp.FrequencyDomainFIRFilter('NumeratorDomain','Frequency',...'FrequencyResponse', H,...'NumeratorLength',numel(imp),...'Method','overlap-add'); fprintf('Frequency domain filter latency is %d samples\n',oa.Latency);
频域滤波延迟是402 samples

Create adsp.FIRFilterSystem object? and specify the numerator as the time-domain coefficients computed using thefir1function,imp. Delay the FIR output by the latency of the frequency-domain FIR filter.

fir = dsp.FIRFilter('Numerator',imp); dly = dsp.Delay('Length',oa.Latency);

Create twodsp.SineWaveobjects. The sine waves generated have a sample rate of 8000 Hz, frame size of 256, and frequencies of 100 Hz and 3 kHz, respectively. Create atimescopeobject to view the filtered outputs.

frameLen = 256; sin_100Hz = dsp.SineWave('Frequency', 100,'SampleRate',Fs,...'SamplesPerFrame',frameLen); sin_3KHz = dsp.SineWave('Frequency',3e3,'SampleRate',Fs,...'SamplesPerFrame',frameLen); ts = timescope('TimeSpanOverrunAction','Scroll',...'ShowGrid',true,'YLimits',[-1.1 1.1],...'TimeSpanSource','Property','TimeSpan',5 * frameLen/Fs,...'ShowLegend',true,...'SampleRate',Fs,...'ChannelNames',{'Overlap-add','Direct-form FIR'});

Streaming

Stream 1e4 frames of noisy input data. Pass this data through the frequency-domain FIR filter and the time-domain FIR filter. View the filtered outputs in the time scope.

numFrames = 1e4;foridx = 1:numFrames x = sin_100Hz() + sin_3KHz() + 0.01 * randn(frameLen,1); y1 = oa(x); y2 = fir(dly(x)); ts([y1,y2]);end

The outputs of both the filters match exactly.

Algorithms

expand all

Overlap-save and overlap-add are the two frequency-domain FFT-based filtering methods this algorithm uses.

References

[1] Stockham, T. G., Jr. "High Speed Convolution and Correlation."Proceedings of the 1966 Spring Joint Computer Conference, AFIPS,Vol 28, 1966, pp. 229–233.

Extended Capabilities

Version History

Introduced in R2017b