bandpass - Bandpass-filter signals - MATLAB (original) (raw)

Syntax

Description

[y](#d126e3697) = bandpass([x](#d126e3456),[wpass](#d126e3478)) filters the input signal x using a bandpass filter with a passband frequency range specified by the two-element vectorwpass and expressed in normalized units of_π_ rad/sample. bandpass uses a minimum-order filter with a stopband attenuation of 60 dB and compensates for the delay introduced by the filter. If x is a matrix, the function filters each column independently.

[y](#d126e3697) = bandpass([x](#d126e3456),[fpass](#d126e3499),[fs](#d126e3526)) specifies that x has been sampled at a rate offs hertz. The two-element vectorfpass specifies the passband frequency range of the filter in hertz.

example

[y](#d126e3697) = bandpass([xt](#d126e3534),[fpass](#d126e3499)) bandpass-filters the data in timetable xt using a filter with a passband frequency range specified in hertz by the two-element vectorfpass. The function independently filters all variables in the timetable and all columns inside each variable.

[y](#d126e3697) = bandpass(___,[Name=Value](#namevaluepairarguments)) specifies additional options for any of the previous syntaxes using name-value arguments. You can change the stopband attenuation, the Bandpass Filter Steepness, and the type of impulse response of the filter.

example

bandpass(___) with no output arguments plots the input signal and overlays the filtered signal.

Examples

collapse all

Create a signal sampled at 1 kHz for 1 second. The signal contains three tones, one at 50 Hz, another at 150 Hz, and a third at 250 Hz. The high-frequency and low-frequency tones both have twice the amplitude of the intermediate tone. The signal is embedded in Gaussian white noise of variance 1/100.

fs = 1e3; t = 0:1/fs:1; x = [2 1 2]sin(2pi*[50 150 250]'.*t) + randn(size(t))/10;

Bandpass-filter the signal to remove the low-frequency and high-frequency tones. Specify passband frequencies of 100 Hz and 200 Hz. Display the original and filtered signals, and also their spectra.

Figure contains 2 axes objects. Axes object 1 with title Bandpass Filtering (Fpass = [100 200] Hz), xlabel Time (s) contains 2 objects of type line. These objects represent Original, Filtered. Axes object 2 with xlabel Frequency (Hz), ylabel Power Spectrum (dB) contains 2 objects of type line. These objects represent Original, Filtered.

Implement a basic digital music synthesizer and use it to play a traditional song. Specify a sample rate of 2 kHz. Plot the spectrogram of the song.

fs = 2e3; t = 0:1/fs:0.3-1/fs; fq = [-Inf -9:2]/12; note = @(f,g) [1 1 1]sin(2pi4402.^[fq(g)-1 fq(g) fq(f)+1]'.*t);

mel = [5 3 1 3 5 5 5 0 3 3 3 0 5 8 8 0 5 3 1 3 5 5 5 5 3 3 5 3 1]+1; acc = [5 0 8 0 5 0 5 5 3 0 3 3 5 0 8 8 5 0 8 0 5 5 5 0 3 3 5 0 1]+1;

song = []; for kj = 1:length(mel) song = [song note(mel(kj),acc(kj)) zeros(1,0.01*fs)]; end song = song/(max(abs(song))+0.1);

% To hear, type sound(song,fs)

pspectrum(song,fs,"spectrogram",TimeResolution=0.31, ... OverlapPercent=0,MinThreshold=-60)

Figure contains an axes object. The axes object with title Fres = 8.2798 Hz, Tres = 310 ms, xlabel Time (s), ylabel Frequency (kHz) contains an object of type image.

Bandpass-filter the signal to separate the middle register from the other two. Specify passband frequencies of 230 Hz and 450 Hz. Plot the original and filtered signals in the time and frequency domains.

pong = bandpass(song,[230 450],fs);

% To hear, type sound(pong,fs)

bandpass(song,[230 450],fs)

Figure contains 2 axes objects. Axes object 1 with title Bandpass Filtering (Fpass = [230 450] Hz), xlabel Time (s) contains 2 objects of type line. These objects represent Original, Filtered. Axes object 2 with xlabel Frequency (kHz), ylabel Power Spectrum (dB) contains 2 objects of type line. These objects represent Original, Filtered.

Plot the spectrogram of the middle register.

figure pspectrum(pong,fs,"spectrogram",TimeResolution=0.31, ... OverlapPercent=0,MinThreshold=-60)

Figure contains an axes object. The axes object with title Fres = 8.2798 Hz, Tres = 310 ms, xlabel Time (s), ylabel Frequency (kHz) contains an object of type image.

Filter white noise sampled at 1 kHz using an infinite impulse response bandpass filter with a passband width of 100 Hz. Use different steepness values. Plot the spectra of the filtered signals.

fs = 1000; x = randn(20000,1);

[y1,d1] = bandpass(x,[ 50 150],fs,ImpulseResponse="iir",Steepness=0.5); [y2,d2] = bandpass(x,[200 300],fs,ImpulseResponse="iir",Steepness=0.8); [y3,d3] = bandpass(x,[350 450],fs,ImpulseResponse="iir",Steepness=0.95);

pspectrum([y1 y2 y3],fs) legend("Steepness = " + [0.5 0.8 0.95],Location="south")

Figure contains an axes object. The axes object with title Fres = 1.7341 Hz, xlabel Frequency (Hz), ylabel Power Spectrum (dB) contains 3 objects of type line. These objects represent Steepness = 0.5, Steepness = 0.8, Steepness = 0.95.

Compute and plot the frequency responses of the filters.

[h1,f] = freqz(d1,1024,fs); [h2,] = freqz(d2,1024,fs); [h3,] = freqz(d3,1024,fs);

plot(f,mag2db(abs([h1 h2 h3]))) legend("Steepness = " + [0.5 0.8 0.95],Location="south") ylim([-100 10])

Figure contains an axes object. The axes object contains 3 objects of type line. These objects represent Steepness = 0.5, Steepness = 0.8, Steepness = 0.95.

Make the filters asymmetric by specifying different values of steepness at the lower and higher passband frequencies.

[y1,d1] = bandpass(x,[ 50 150],fs,ImpulseResponse="iir",Steepness=[0.5 0.8]); [y2,d2] = bandpass(x,[200 300],fs,ImpulseResponse="iir",Steepness=[0.5 0.8]); [y3,d3] = bandpass(x,[350 450],fs,ImpulseResponse="iir",Steepness=[0.5 0.8]);

pspectrum([y1 y2 y3],fs)

Figure contains an axes object. The axes object with title Fres = 1.7341 Hz, xlabel Frequency (Hz), ylabel Power Spectrum (dB) contains 3 objects of type line.

Compute and plot the frequency responses of the filters.

[h1,f] = freqz(d1,1024,fs); [h2,] = freqz(d2,1024,fs); [h3,] = freqz(d3,1024,fs);

plot(f,mag2db(abs([h1 h2 h3]))) ylim([-100 10])

Figure contains an axes object. The axes object contains 3 objects of type line.

Input Arguments

collapse all

Input signal, specified as a vector or matrix.

Example: sin(2*pi*(0:127)/16)+randn(1,128)/100 specifies a noisy sinusoid

Example: [2 1].*sin(2*pi*(0:127)'./[16 64]) specifies a two-channel sinusoid.

Data Types: single | double
Complex Number Support: Yes

Normalized passband frequency range, specified as a two-element vector with elements in the interval (0, 1).

Passband frequency range, specified as a two-element vector with elements in the interval (0, fs/2).

Sample rate, specified as a positive real scalar.

Input timetable. xt must contain increasing, finite, and equally spaced row times of type duration in seconds.

If a timetable has missing or duplicate time points, you can fix it using the tips in Clean Timetable with Missing, Duplicate, or Nonuniform Times.

Example: timetable(seconds(0:4)',randn(5,1),randn(5,2)) contains a single-channel random signal and a two-channel random signal, sampled at 1 Hz for 4 seconds.

Example: timetable(randn(5,1),randn(5,2),SampleRate=1) contains a single-channel random signal and a two-channel random signal, sampled at 1 Hz for 4 seconds.

Name-Value Arguments

collapse all

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: ImpulseResponse="iir",StopbandAttenuation=30 filters the input using a minimum-order IIR filter that attenuates by 30 dB the frequencies smaller than fpass(1) and the frequencies larger thanfpass(2).

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: 'ImpulseResponse','iir','StopbandAttenuation',30 filters the input using a minimum-order IIR filter that attenuates by 30 dB the frequencies smaller than fpass(1) and the frequencies larger thanfpass(2).

Type of impulse response of the filter, specified as "fir","iir", or "auto".

Transition band steepness, specified as a scalar or two-element vector with elements in the interval [0.5, 1). As the steepness increases, the filter response approaches the ideal bandpass response, but the resulting filter length and the computational cost of the filtering operation also increase. SeeBandpass Filter Steepness for more information.

Filter stopband attenuation, specified as a positive scalar in dB.

Output Arguments

collapse all

Filtered signal, returned as a vector, a matrix, or a timetable with the same dimensions as the input.

Bandpass filter used in the filtering operation, returned as a digitalFilter object.

More About

collapse all

The Steepness argument controls the width of a filter's transition regions. The lower the steepness, the wider the transition region. The higher the steepness, the narrower the transition region.

To interpret the filter steepness, consider the following definitions:

To control the width of the transition bands, you can specifySteepness as either a two-element vector, [_s_lower,_s_upper], or a scalar.

When you specify Steepness as a vector, the function:

When you specify Steepness as a scalar, the function:

The default value of Steepness is[0.85 0.85].

Version History

Introduced in R2018a