Main Content

tffilt

Time-frequency filtering using binary mask and Gabor transform

Since R2025a

    Description

    y = tffilt(bmask,x) reconstructs the filtered signal y by applying the time-frequency binary mask bmask to the discrete Gabor transform (DGT) of x and inverting the result.

    y = tffilt(bmask,x,Name=Value) specifies options using or more name-value arguments, in addition to the input arguments of the previous syntax. For example, to specify a hop length of 16 samples, set HopLength to 16.

    example

    Examples

    collapse all

    Create a signal that consists of a quadratic chirp and two sinusoids whose frequencies are 250 Hz and 350 Hz, respectively. The sinusoids have disjoint time support. Sample the signal at 1 kHz for four seconds.

    tspan = 4;
    Fs = 1e3;
    t = 0:1/Fs:tspan-1/Fs;
    chp = chirp(tspan/2-t,30,max(tspan/2-t),100,"quadratic",[],"concave");
    si1 = cos(250*2*pi*t);
    si2 = cos(350*2*pi*t);
    si1 = si1.*(t<tspan/2);
    si2 = si2.*(t>=tspan/2);
    sig = si1+si2+chp;

    Visualize the one-sided discrete Gabor transform of the signal.

    dgt(sig,SampleRate=Fs,FrequencyRange="onesided")

    Figure contains an axes object. The axes object with title Discrete Gabor Transform, xlabel Time (s), ylabel Frequency (Hz) contains an object of type image.

    Obtain the DGT of the signal. Also obtain the frequencies and times at which the DGT is evaluated.

    [d,frq,tm] = dgt(sig,SampleRate=Fs,FrequencyRange="onesided");

    Use the frequency vector and time vector to create time-frequency masks that mark for removal:

    • The 250 Hz sinusoid.

    • The chirp samples from two to four seconds.

    frqSinusoid = (frq>225)&(frq<275);
    tmSinusoid = (tm<2);
    mskSinusoid = frqSinusoid*tmSinusoid';
    
    frqChirp = (frq<125);
    tmChirp = (tm>2);
    mskChirp = frqChirp*tmChirp';

    Use the tffilt function to reconstruct a filtered signal using the two masks. Specify the "gm" time-frequency filtering method.

    rec = tffilt({mskSinusoid,mskChirp},sig,FrequencyRange="onesided", ...
        Method="gm");

    Plot the original signal and reconstruction.

    tiledlayout(2,1)
    nexttile
    plot(t,sig)
    ylim([-2.2 2.2])
    ylabel("Amplitude")
    title("Original Signal")
    nexttile
    plot(t,rec)
    ylim([-2.2 2.2])
    ylabel("Amplitude")
    xlabel("Time (s)")
    title("Filtered Signal")

    Figure contains 2 axes objects. Axes object 1 with title Original Signal, ylabel Amplitude contains an object of type line. Axes object 2 with title Filtered Signal, xlabel Time (s), ylabel Amplitude contains an object of type line.

    Visualize the DGT of the filtered signal.

    figure
    dgt(rec,SampleRate=Fs,FrequencyRange="onesided")

    Figure contains an axes object. The axes object with title Discrete Gabor Transform, xlabel Time (s), ylabel Frequency (Hz) contains an object of type image.

    Load the harmperc data file. After loading, your workspace contains the following variables:

    • x — A mixed audio recording of a drum and guitar.

    • harm — An audio recording of only the guitar.

    • fs — A scalar containing the sample rate.

    The duration of both recordings is six seconds. The sample rate is 16 kHz.

    load harmperc

    Use dgt to visualize the one-sided DGT of the mixed recording. Specify a window length of 1024 samples, a hop length of 512 samples. Set the number of frequency bins to 211.

    winLen = 1024;
    hopLen = 512;
    numBins = 2^11;
    dgt(x,WindowLength=winLen,HopLength=hopLen, ...
        SampleRate=fs, ...
        NumFrequencyBins=numBins, ...
        FrequencyRange="onesided")

    Figure contains an axes object. The axes object with title Discrete Gabor Transform, xlabel Time (s), ylabel Frequency (kHz) contains an object of type image.

    The difference between the mixed and guitar recordings is the percussive audio. Visualize the one-sided DGT of the difference between the two recordings. Use the same dgt parameters.

    dgt(x-harm,WindowLength=winLen,HopLength=hopLen, ...
        SampleRate=fs, ...
        NumFrequencyBins=numBins, ...
        FrequencyRange="onesided")

    Figure contains an axes object. The axes object with title Discrete Gabor Transform, xlabel Time (s), ylabel Frequency (kHz) contains an object of type image.

    Obtain the DGT of the difference between the two recordings and the mixed recording.

    Dp = dgt(x-harm,WindowLength=winLen,HopLength=hopLen, ...
        SampleRate=fs, ...
        NumFrequencyBins=numBins, ...
        FrequencyRange="onesided");
    Dx = dgt(x,WindowLength=winLen,HopLength=hopLen, ...
        SampleRate=fs, ...
        NumFrequencyBins=numBins, ...
        FrequencyRange="onesided");

    Use both DGTs to create a binary mask that identifies the time-frequency bins associated with the percussive audio to filter out of the DGT of the mixed recording. Keep in mind that a true value indicates that tffilt filters out the corresponding time-frequency bin.

    bmask = abs(Dp)>0.5*abs(Dx);

    Use tffilt to apply the mask to the mixed audio recording. Use the "gm" time-frequency filtering method. Visualize the DGT of the reconstruction.

    y = tffilt(bmask,x,WindowLength=winLen,HopLength=hopLen, ...
        NumFrequencyBins=numBins,FrequencyRange="onesided",Method="gm");
    dgt(y,WindowLength=winLen,HopLength=hopLen, ...
        SampleRate=fs, ...
        NumFrequencyBins=numBins,FrequencyRange="onesided");

    Figure contains an axes object. The axes object with title Discrete Gabor Transform, xlabel Time (s), ylabel Frequency (kHz) contains an object of type image.

    Compute the signal-to-interference (SIR) before and after the filtering.

    sirBefore = 20*log10(norm(harm,2)/norm(harm-x,2))
    sirBefore = 
    10.4058
    
    sirAfter = 20*log10(norm(harm,2)/norm(harm-y,2))
    sirAfter = 
    17.2166
    

    Input Arguments

    collapse all

    Binary mask, specified as a logical matrix or a cell array of logical matrices. The size of each logical matrix must be the same as the size of the DGT of the input signal x. The row and column dimensions correspond to the frequency and time axes, respectively, of the time-frequency plane. A true value indicates that tffilt filters out the corresponding time-frequency bin.

    To make the size of the DGT the same as that of bmask, HopLength, NumFrequencyBins, and FrequencyRange must be the same as those used in computing bmask.

    If bmask is a cell array, the tffilt function applies each mask sequentially to the DGT of x before reconstructing the filtered signal.

    Data Types: logical

    Input signal, specified as a vector or a timetable with a single variable containing a vector. If x is a timetable, it must contain finite and uniformly increasing row times.

    To obtain the DGT of the input signal, x, the tffilt function internally uses the dgt function. If the signal length is not an integer multiple of the least common multiple (LCM) of the hop length, HopLength, and the length of the Gaussian window, WindowLength, dgt zero-pads the signal to the nearest largest length that is a multiple of this LCM.

    Data Types: single | double
    Complex Number Support: Yes

    Name-Value Arguments

    collapse all

    Specify optional pairs of arguments as Name1=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: y=tffilt(bmask,x,FrequencyRange="onesided",HopLength=32) computes a one-sided DGT using a hop length of 32 samples.

    Gaussian window length in samples, specified as a positive integer. The window length also determines the time-frequency ratio (TFR) of the window. For more information, see Time-Frequency Ratio.

    Data Types: single | double

    Hop length or time shift of the Gaussian windows in samples, specified as a nonnegative integer. The hop length affects the overlap between the windows and thus the time resolution of the transform.

    You must specify the same hop length to create the binary mask bmask.

    Data Types: single | double

    Number of frequency bins to use to calculate the DGT, specified as a positive integer. The number of bins determines the frequency resolution in the time-frequency representation of the signal.

    To ensure the transform is redundant and perfect reconstruction can be achieved, the number of frequency bins must be larger than the hop length.

    You must specify the same number of frequency bins to create the binary mask bmask.

    Data Types: single | double

    DGT frequency range, specified as "centered", "onesided", or "twosided". The tffilt function computes the DGT over the specified range.

    • "centered" — Computes a two-sided and centered DGT.

    • "onesided" — Computes a one-sided DGT.

    • "twosided" — Computes a two-sided DGT.

    You must use the same number of frequency bins and frequency range to create the binary mask bmask.

    Time-frequency filtering method, specified as one of these:

    • "igm"tffilt performs filtering by reconstructing the signal using the inverse of the Gabor multiplier. This method involves solving a linear system where the Gabor multiplier's eigenvalues are estimated using the iterative preconditioned conjugate gradient technique, allowing effective reconstruction of the filtered signal. tffilt internally uses the pcg and eigs functions.

    • "gm"tffilt performs filtering by applying the binary mask bmask directly to the DGT of the input signal x, and then reconstructing the signal using the inverse DGT. Specifically, the algorithm calculates the filtered signal as IDGT(bmask.*dgt(x)). This method is computationally less intensive compared to the other methods.

    • "rigm"tffilt performs filtering by solving a regularized optimization problem [1]. The function computes the eigenvalues of the Gabor multiplier using the adaptive randomized range finder (ARRF) techniques and the Nystrom method for random eigenvalue decomposition. To learn more about the ARRF and Nystrom algorithms, as implemented in tffilt, see Algorithms 4.2 and 5.5, respectively, in [4].

    For more information, see Gabor Multipliers.

    Output Arguments

    collapse all

    Filtered signal, returned as a vector or timetable. y has the same size and data type as the input signal x.

    More About

    collapse all

    References

    [1] Krémé, A. Marina, Valentin Emiya, Caroline Chaux, and Bruno Torrésani. “Time-Frequency Fading Algorithms Based on Gabor Multipliers.” IEEE Journal of Selected Topics in Signal Processing 15, no. 1 (January 2021): 65–77. https://doi.org/10.1109/JSTSP.2020.3045938.

    [2] Mallat, S.G. and Zhifeng Zhang. “Matching Pursuits with Time-Frequency Dictionaries.” IEEE Transactions on Signal Processing 41, no. 12 (December 1993): 3397–3415. https://doi.org/10.1109/78.258082.

    [3] Søndergaard, Peter. “An Efficient Algorithm for the Discrete Gabor Transform Using Full Length Windows.” In SAMPTA ’09 International Conference on SAMPling Theory and Applications, edited by Laurent Fesquet and Bruno Torresani, 223–26. Marseille, France, 2009. https://hal.science/hal-00495456/file/SampTAProceedings.pdf.

    [4] Halko, N., P. G. Martinsson, and J. A. Tropp. “Finding Structure with Randomness: Probabilistic Algorithms for Constructing Approximate Matrix Decompositions.” SIAM Review 53, no. 2 (January 2011): 217–88. https://doi.org/10.1137/090771806.

    Extended Capabilities

    expand all

    C/C++ Code Generation
    Generate C and C++ code using MATLAB® Coder™.

    Version History

    Introduced in R2025a