getMask
Description
Examples
Label Gaussian atoms in the time-frequency domain using a time-frequency region-of-interest (ROI) label definition and spectrogram options. Get mask from labeled signal in the time-frequency domain.
Generate Signal and Visualize Spectrogram
Generate a signal that consists of a voltage-controlled oscillator and five Gaussian atoms. The signal is sampled at 14 kHz for two seconds. Plot the spectrogram of the signal.
Fs = 14000; t = (0:1/Fs:2)'; st = 0.01; gaussFun = @(A,x,mu,f) exp(-(x-mu).^2/(2*st^2)).*sin(2*pi*f.*x)*A'; atomTimeCenters = [0.2 0.5 1 1.3 1.8]; atomFreqCenters = [2 6 2 5 1]*1000; atomAmplitudes = [1 1 1 1 1]/10; s = gaussFun(atomAmplitudes,t,atomTimeCenters,atomFreqCenters) ... + vco(chirp(t+0.1,0,t(end),3).*exp(-2*(t-1).^2),[0.1 0.4]*Fs,Fs); bt = 0.2; tr = 0.05; op = 99; pspectrum(s,Fs,"spectrogram", ... Leakage=bt,TimeResolution=tr,OverlapPercent=op)

The spectrogram shows four patches in time-frequency domain that correspond with the Gaussian atoms. Define the times and frequencies for all the atoms.
atomTimes = atomTimeCenters'+[-st st]*5.5; atomFreqs = atomFreqCenters'+[-1 1]*200;
Label Signal in Time-Frequency Domain
Create a numeric time-frequency ROI label definition to label the Gaussian atoms. Specify spectrogram options with leakage properties.
opts = labelSpectrogramOptions("leakage", ... Leakage=40*(1-bt),Overlap=op, ... TimeResolutionMode="specify",TimeResolution=tr); lblDef = signalLabelDefinition("Atom", ... LabelDataType="numeric", ... LabelType="roiTimeFrequency",TimeFrequencyOptions=opts);
Create a labeled signal set from the signal and time-frequency ROI label definition.
lss = labeledSignalSet(s,lblDef,SampleRate=Fs);
Label the atoms in time-frequency domain. Set the label values to 1 through 5.
setLabelValue(lss,1,"Atom",atomTimes,atomFreqs,1:5)Display the label table. Each label has its time-frequency ROI limits and a numeric value.
lblTable = lss.Labels.Atom{1};
lblTable.Value = cell2mat(lblTable.Value)lblTable=5×3 table
TimeROILimits FrequencyROILimits Value
______________ __________________ _____
0.145 0.255 1800 2200 1
0.445 0.555 5800 6200 2
0.945 1.055 1800 2200 3
1.245 1.355 4800 5200 4
1.745 1.855 800 1200 5
Get and Visualize Array of Label Masks
Create a time-frequency mask with the label table and spectrogram options. Specify the signal sample rate and mask format.
msk = timeFrequencyMask(lblTable,opts, ... SampleRate=Fs,MaskFormat="pixel",OverlapAction="prioritizeByList");
Read the time-frequency mask and get the array of label regions as a per-pixel matrix and as a 3-D binary array.
lblPerPixel = getMask(msk,s,PixelType="perpixel"); lblBinaries = getMask(msk,s,PixelType="binary");
Plot the array of labels in per-pixel format. The white rectangular patches represent time-frequency regions where a label exists.
figure tiledlayout("flow") nexttile imshow(~isnan(lblPerPixel)) title("2-D per-pixel mask (grayscale image)")
Plot the first three pages of the array of labels in binary format. Each page consists of pixels marked with true (1) or false (0), where true (1) represents a labeled time-frequency ROI. The three-page 3-D array forms an image with colored rectangular patches that depict the labeled ROIs:
Red patch (first page of array of labels), represents the first time-frequency ROI, labeled as
1.Green patch (second page of array of labels), represents the second time-frequency ROI, labeled as
2.Blue patch (third page of array of labels), represents the third time-frequency ROI, labeled as
3.
nexttile
imshow(double(lblBinaries(:,:,1:3)))
title("3-D binary mask (RGB image)")
Input Arguments
Time-frequency mask, specified as a timeFrequencyMask object.
Example: msk = timeFrequencyMask(table([0.1 0.2],[160 180],true),labelSpectrogramOptions,SampleRate=1000) specifies a time-frequency mask with a true time-frequency region that spans from 0.1 to 0.2 seconds and from 160 to 180 Hz, where the time-frequency map of the signal is computed using default label spectrogram options.
Example: msk = timeFrequencyMask(table([10 20;120 150;160 190],[0.16 0.18;0.08
0.14;0.2
0.22],categorical(["whale1";"whale2";"whale1"])),
labelSpectrogramOptions("rbw"))
specifies a time-frequency mask with two
whale1 time-frequency
regions and one whale2
time-frequency region, where the time-frequency map
of the signal is computed using resolution-bandwidth
(RBW) label spectrogram options. The time limits are
in samples and the frequency limits are in
radians/sample.
Input signal, specified as a vector.
Example: chirp(0:1/1e3:1,25,1,50) specifies a chirp sampled at 1
kHz.
Data Types: single | double
Complex Number Support: Yes
Pixel format for array of label masks, specified as "perpixel" or
"binary".
When you specify
mskwhere the mask format () ismsk.MaskFormat"pixel", you can returnlblas a matrix of label values or as a 3-D array of binary values depending on the value ofpt."perpixel"—getMaskreturnslblas a matrix and assigns a label value to each pixel. The function assigns values to overlapping according to the value of.msk.MaskPriority"binary"—getMaskreturnslblas a 3-D array, where each page corresponds to a logical or categorical label, and each true pixel indicates a label value to that page.
If you specify
mskwhereis other thanmsk.MaskFormat"pixel", thengetMaskignores the value specified inpt.
For example, assume three categorical labels: "b" (blue),
"o" (orange), and "g" (green). When you specify
lbl = getMask(msk,x,PixelType=pt), the
getMask function returns lbl as a matrix
or array, as shown in this image.
Data Types: char | string
Output Arguments
Array of label masks, returned as a matrix, 3-D array, cell array, or table. The
output lbl lists each time-frequency region of interest (ROI) and
the corresponding label value.
The type of output depends on the value of
:msk.MaskFormat
If
ismsk.MaskFormat"pixel", thengetMaskreturnslblas a matrix or 3-D array with the same number of rows and columns as the time-frequency map,S. The function returnslblas a matrix or 3-D array depending on the value ofpt.If
ismsk.MaskFormat"boundingbox", thengetMaskreturnslblas a 1-by-2 cell array.The first element of the cell array contains the indices associated with the times and frequencies for each time-frequency region.
The second element of the cell array contains the label value for each time-frequency region.
If
ismsk.MaskFormat"table", thengetMaskreturnslblas an N-by-3 table.N is the number of time-frequency regions.
The variables comprise the time ROI limits, frequency ROI limits, and label value.
Time-frequency map, returned as a matrix.
The
plotMaskfunction uses thelabelSpectrogramOptionsobject stored into compute the time-frequency map and return it inmsk.TimeFrequencyOptionsS.If
is true, thenmsk.SpecifyMaskSizeplotMaskuses the value stored into resizemsk.MaskSizeSto the specified size.
Version History
Introduced in R2026a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)