main content

classify sounds in audio signal -凯发k8网页登录

classify sounds in audio signal

since r2020b

description

example

sounds = classifysound(audioin,fs) returns the sound classes detected over time in the audio input, audioin, with sample rate fs.

example

sounds = classifysound(audioin,fs,name,value) specifies options using one or more name,value pair arguments.

example: sounds = classifysound(audioin,fs,'specificitylevel','low') classifies sounds using low specificity.

example

[sounds,timestamps] = classifysound(___) also returns time stamps associated with each detected sound.

example

[sounds,timestamps,resultstable] = classifysound(___) also returns a table containing result details.

example

classifysound(___) with no output arguments creates a word cloud of the identified sounds in the audio signal.

this function requires both audio toolbox™ and deep learning toolbox™.

examples

download and unzip the audio toolbox™ support for yamnet.

if the audio toolbox support for yamnet is not installed, then the first call to the function provides a link to the download location. to download the model, click the link. unzip the file to a location on the matlab path.

alternatively, execute the following commands to download and unzip the yamnet model to your temporary directory.

downloadfolder = fullfile(tempdir,'yamnetdownload');
loc = websave(downloadfolder,'https://ssd.mathworks.com/supportfiles/audio/yamnet.zip');
yamnetlocation = tempdir;
unzip(loc,yamnetlocation)
addpath(fullfile(yamnetlocation,'yamnet'))

generate 1 second of pink noise assuming a 16 khz sample rate.

fs = 16e3;
x = pinknoise(fs);

call classifysound with the pink noise signal and the sample rate.

identifiedsound = classifysound(x,fs)
identifiedsound = 
"pink noise"

read in an audio signal. call classifysound to return the detected sounds and corresponding time stamps.

[audioin,fs] = audioread('multiplesounds-16-16-mono-18secs.wav');
[sounds,timestamps] = classifysound(audioin,fs);

plot the audio signal and label the detected sound regions.

t = (0:numel(audioin)-1)/fs;
plot(t,audioin)
xlabel('time (s)')
axis([t(1),t(end),-1,1])
textheight = 1.1;
for idx = 1:numel(sounds)
    patch([timestamps(idx,1),timestamps(idx,1),timestamps(idx,2),timestamps(idx,2)], ...
        [-1,1,1,-1], ...
        [0.3010 0.7450 0.9330], ...
        'facealpha',0.2);
    text(timestamps(idx,1),textheight 0.05*(-1)^idx,sounds(idx))
end

select a region and listen only to the selected region.

samplestamps = floor(timestamps*fs) 1;
soundevent = 3;
isolatedsoundevent = audioin(samplestamps(soundevent,1):samplestamps(soundevent,2));
sound(isolatedsoundevent,fs);
display('detected sound = '   sounds(soundevent))
    "detected sound = snoring"

read in an audio signal containing multiple different sound events.

[audioin,fs] = audioread('multiplesounds-16-16-mono-18secs.wav');

call classifysound with the audio signal and sample rate.

[sounds,~,soundtable] = classifysound(audioin,fs);

the sounds string array contains the most likely sound event in each region.

sounds
sounds = 1×5 string
    "stream"    "machine gun"    "snoring"    "bark"    "meow"

the soundtable contains detailed information regarding the sounds detected in each region, including score means and maximums over the analyzed signal.

soundtable
soundtable=5×2 table
       timestamps         results  
    ________________    ___________
         0      3.92    {4×3 table}
    4.0425    6.0025    {3×3 table}
      6.86    9.1875    {2×3 table}
    10.658    12.373    {4×3 table}
    12.985     16.66    {4×3 table}

view the last detected region.

soundtable.results{end}
ans=4×3 table
             sounds             averagescores    maxscores
    ________________________    _____________    _________
    "animal"                       0.79514        0.99941 
    "domestic animals, pets"       0.80243        0.99831 
    "cat"                           0.8048        0.99046 
    "meow"                          0.6342        0.90177 

call classifysound again. this time, set includedsounds to animal so that the function retains only regions in which the animal sound class is detected.

[sounds,timestamps,soundtable] = classifysound(audioin,fs, ...
    'includedsounds','animal');

the sounds array only returns sounds specified as included sounds. the sounds array now contains two instances of animal that correspond to the regions declared as bark and meow previously.

sounds
sounds = 1×2 string
    "animal"    "animal"

the sound table only includes regions where the specified sound classes were detected.

soundtable
soundtable=2×2 table
       timestamps         results  
    ________________    ___________
    10.658    12.373    {4×3 table}
    12.985     16.66    {4×3 table}

view the last detected region in soundtable. the results table still includes statistics for all detected sounds in the region.

soundtable.results{end}
ans=4×3 table
             sounds             averagescores    maxscores
    ________________________    _____________    _________
    "animal"                       0.79514        0.99941 
    "domestic animals, pets"       0.80243        0.99831 
    "cat"                           0.8048        0.99046 
    "meow"                          0.6342        0.90177 

to explore which sound classes are supported by classifysound, use .

read in an audio signal and call classifysound to inspect the most likely sounds arranged in chronological order of detection.

[audioin,fs] = audioread("multiplesounds-16-16-mono-18secs.wav");
sounds = classifysound(audioin,fs)
sounds = 1×5 string
    "stream"    "machine gun"    "snoring"    "bark"    "meow"

call classifysound again and set excludedsounds to meow to exclude the sound meow from the results. the segment previously classified as meow is now classified as cat, which is its immediate predecessor in the audioset ontology.

sounds = classifysound(audioin,fs,"excludedsounds","meow")
sounds = 1×5 string
    "stream"    "machine gun"    "snoring"    "bark"    "cat"

call classifysound again, and set excludedsounds to cat. when you exclude a sound, all successors are also excluded. this means that excluding the sound cat also excludes the sound meow. the segment originally classified as meow is now classified as domestic animals, pets, which is the immediate predecessor to cat in the audioset ontology.

sounds = classifysound(audioin,fs,"excludedsounds","cat")
sounds = 1×5 string
    "stream"    "machine gun"    "snoring"    "bark"    "domestic animals, pets"

call classifysound again and set excludedsounds to domestic animals, pets. the sound class, domestic animals, pets is a predecessor to both bark and meow, so by excluding it, the sounds previously identified as bark and meow are now both identified as the predecessor of domestic animals, pets, which is animal.

sounds = classifysound(audioin,fs,"excludedsounds","domestic animals, pets")
sounds = 1×5 string
    "stream"    "machine gun"    "snoring"    "animal"    "animal"

call classifysound again and set excludedsounds to animal. the sound class animal has no predecessors.

sounds = classifysound(audioin,fs,"excludedsounds","animal")
sounds = 1×3 string
    "stream"    "machine gun"    "snoring"

if you want to avoid detecting meow and its predecessors, but continue detecting successors under the same predecessors, use the includedsounds option. call yamnetgraph to get a list of all supported classes. remove meow and its predecessors from the array of all classes, and then call classifysound again.

[~,classes] = yamnetgraph;
classestoinclude = setxor(classes,["meow","cat","domestic animals, pets","animal"]);
sounds = classifysound(audioin,fs,"includedsounds",classestoinclude)
sounds = 1×4 string
    "stream"    "machine gun"    "snoring"    "bark"

read in an audio signal and listen to it.

[audioin,fs] = audioread('multiplesounds-16-16-mono-18secs.wav');
sound(audioin,fs)

call classifysound with no output arguments to generate a word cloud of the detected sounds.

classifysound(audioin,fs);

modify default parameters of classifysound to explore the effect on the word cloud.

threshold = 0.1;
minimumsoundseparation = 0.92;
minimumsoundduration = 1.02;
classifysound(audioin,fs, ...
    'threshold',threshold, ...
    'minimumsoundseparation',minimumsoundseparation, ...
    'minimumsoundduration',minimumsoundduration);

input arguments

audio input, specified as a one-channel signal (column vector).

data types: single | double

sample rate in hz, specified as a positive scalar.

data types: single | double

name-value arguments

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.

before r2021a, use commas to separate each name and value, and enclose name in quotes.

example: 'threshold',0.1

confidence threshold for reporting sounds, specified as the comma-separated pair consisting of 'threshold' and a scalar in the range (0,1).

data types: single | double

minimum separation between consecutive regions of the same detected sound in seconds, specified as the comma-separated pair consisting of 'minimumsoundseparation' and a positive scalar. regions closer than the minimum sound separation are merged.

data types: single | double

minimum duration of detected sound regions in seconds, specified as the comma-separated pair consisting of 'minimumsoundduration' and a positive scalar. regions shorter than the minimum sound duration are discarded.

data types: single | double

sounds to include in results, specified as the comma-separated pair consisting of 'includedsounds' and a character vector, cell array of character vectors, string scalar, or string array. use to inspect and analyze the sounds supported by classifysound. by default, all supported sounds are included.

this option cannot be used with the 'excludedsounds' option.

data types: char | string | cell

sounds to exclude from results, specified as the comma-separated pair consisting of 'excludedsounds' and a character vector, cell array of character vectors, string scalar, or string array. when you specify an excluded sound, any successors of the excluded sound are also excluded. use to inspect valid sound classes and their predecessors and successors according to the audioset ontology. by default, no sounds are excluded.

this option cannot be used with the 'includedsounds' option.

data types: char | string | cell

specificity of reported sounds, specified as the comma-separated pair consisting of 'specificitylevel' and 'high', 'low', or 'none'. set specificitylevel to 'high' to make the function emphasize specific sound classes instead of general categories. set specificitylevel to 'low' to make the function return the most general sound categories instead of specific sound classes. set specificitylevel to 'none' to make the function return the most likely sound, regardless of its specificity.

data types: char | string

output arguments

sounds detected over time in audio input, returned as a string array containing the detected sounds in chronological order.

time stamps associated with detected sounds in seconds, returned as an n-by-2 matrix. n is the number of detected sounds. each row of timestamps contains the start and end times of the detected sound region.

detailed results of sound classification, returned as a table. the number of rows in the table is equal to the number of detected sound regions. the columns are as follows.

  • timestamps –– time stamps corresponding to each analyzed region.

  • results –– table with three variables:

    • sounds –– sounds detected in each region.

    • averagescores –– mean network scores corresponding to each detected sound class in the region.

    • maxscores –– maximum network scores corresponding to each detected sound class in the region.

algorithms

the classifysound function uses yamnet to classify audio segments into sound classes described by the audioset ontology. the classifysound function preprocesses the audio so that it is in the format required by yamnet and postprocesses yamnet's predictions with common tasks that make the results more interpretable.

preprocess

  1. resample audioin to 16 khz and cast to single precision.

  2. buffer into l overlapping segments. each segment is 0.98 seconds and the segments are overlapped by 0.8575 seconds.

  3. pass each segment through a one-sided short time fourier transform using a 25 ms periodic hann window with a 10 ms hop and a 512-point dft. the audio is now represented by a 257-by-96-by-l array, where 257 is the number of bins in the one-sided spectra and 96 is the number of spectra in the spectrograms.

  4. convert the complex spectral values to magnitude and discard phase information.

  5. pass the one-sided magnitude spectrum through a 64-band mel-spaced filter bank and then sum the magnitudes in each band. the audio is now represented by a 96-by-64-by-1-by-l array, where 96 is the number of spectra in the mel spectrogram, 64 is the number of mel bands, and the spectrograms are now spaced along the fourth dimension for compatibility with the yamnet model.

  6. convert the mel spectrograms to a log scale.

prediction

pass the 96-by-64-by-1-by-l array of mel spectrograms through yamnet to return an l-by-521 matrix. the output from yamnet corresponds to confidence scores for each of the 521 sound classes over time.

postprocess

sound event region detection
  1. pass each of the 521 confidence signals through a moving mean filter with a window length of 7.

  2. pass each of the signals through a moving median filter with a window length of 3.

  3. convert the confidence signals to binary masks using the specified threshold.

  4. discard any sound shorter than minimumsoundduration.

  5. merge regions that are closer than minimumsoundseparation.

consolidate overlapping sound regions

consolidate identified sound regions that overlap by 50% or more into single regions. the region start time is the smallest start time of all sounds in the group. the region end time is the largest end time of all sounds in the group. the function returns time stamps, sounds classes, and the mean and maximum confidence of the sound classes within the region in the resultstable.

select specificity of sound group

you can set the specificity level of your sound classification using the specificitylevel option. for example, assume there are four sound classes in a sound group with the following corresponding mean scores over the sound region:

  • water –– 0.82817

  • stream –– 0.81266

  • trickle, dribble –– 0.23102

  • pour –– 0.20732

the sound classes, water, stream, trickle, dribble, and pour are situated in audioset ontology as indicated by the graph:

diagram of audioset ontology for water, stream, pour, and trickle, dribble. stream is a successor of water which is a successor of natural sounds. trickle, dribble is a successor of pour which is a successor of liquid which is a successor of sounds of things.

the functions returns the sound class for the sound group in the sounds output argument depending on the specificitylevel:

  • "high" (default) –– in this mode, stream is preferred to water, and trickle, dribble is preferred to pour. stream has a higher mean score over the region, so the function returns stream in the sounds output for the region.

  • "low" –– in this mode, the most general ontological category for the sound class with the highest mean confidence over the region is returned. for trickle, dribble and pour, the most general category is sounds of things. for stream and water, the most general category is natural sounds. because water has the highest mean confidence over the sound region, the function returns natural sounds.

  • "none" –– in this mode, the function returns the sound class with the highest mean confidence score, which in this example is water.

references

[1] gemmeke, jort f., et al. “audio set: an ontology and human-labeled dataset for audio events.” 2017 ieee international conference on acoustics, speech and signal processing (icassp), ieee, 2017, pp. 776–80. doi.org (crossref), doi:10.1109/icassp.2017.7952261.

[2] hershey, shawn, et al. “cnn architectures for large-scale audio classification.” 2017 ieee international conference on acoustics, speech and signal processing (icassp), ieee, 2017, pp. 131–35. doi.org (crossref), doi:10.1109/icassp.2017.7952132.

extended capabilities

version history

introduced in r2020b

see also

apps

blocks

  • | | | | |

functions

网站地图