Search code examples
matlabaudiovolume

How to remove sound below a certain volume level in Matlab


When I load an audio file into Audacity, I get a graph that looks like this:

blah

How do I get a graph like this in MATLAB? I need to extract that middle chunk that looks like a trumpet. My frequency/power graphs won't do the trick.


Solution

  • I'm assuming that graph is in time-domain. As such, you can use the audioread command to read the file:

    [f,fs] = audioread('filename.ext');
    

    filename.ext is the audio file that you want to load in, f will be store the audio data while fs is the sampling frequency. You must make sure that the audio file is in the working directory of where your MATLAB is pointing to before you do this. If you don't, then MATLAB won't be able to find the file. You can override this by specifying the absolute path within the '' of the audioread command (i.e. C:\Documents and Settings\... for Windows or /usr/... for Linux, etc.) Each column of f denotes a single channel in your audio. As such, if f is one column, this denotes mono audio while two columns denote stereo. Assuming your audio is stereo, you can plot the left and right channels by doing:

    figure;
    subplot(2,1,1);
    t = linspace(0,(size(f,1)-1)/fs, size(f,1));
    plot(t, f(:,1));
    title('Left Channel');
    subplot(2,1,2);
    plot(t, f(:,2));
    title('Right Channel');
    

    This will create a figure with two plots. The first plot shows the left channel while the second shows the right channel. The t vector will ensure that each sample that is plotted on either channel has the right time value associated with that particular sample.

    Once you do this, you can use the plot labelling tools in the window to figure out which sample number you need to use to cut out the portions that you need. If I actually had the audio file that you used, I'd be able to help you do this. Otherwise, this code should get you started.