Search code examples
matlabscilab

Can i save many graphs in Matlab as bitmaps with batch processing?


I wish to analyse long audio files in matlab from .wav format, and to export the entire length of the audio as spectrograms in 2048x2048 pixel bitmaps. for one second of sound i would want automate about 20 bitmaps, and i would like to let matlab process and return 1000 bitmaps for a long audio over the course of some hours, and then do further processing in another program.

Is it possible for a beginner?


Solution

  • Here an old SciLab implementation to get you started. I would advice in saving the resulting spectrograms as data, not as image. And write another function which can convert the data to images.

    // For simple plotting, Positions in Milliseconds!!
    function plotASpectrogram( source, startPosition, endPosition )
    
      [audio,sampleRate] = wavread(source);  
    
      startPosition = msecToSamples(startPosition, sampleRate);
      endPosition   = msecToSamples(endPosition, sampleRate);
    
      audio = cropAudio(audio, startPosition, endPosition );
    
      mapsound(audio);//,.1,100,2000,1,sampleRate);
    
      //Label axes   
      startPosition = samplesToMsec(startPosition, sampleRate);  
    
      if(startPosition==0)
        startPosition = "";
      else
        startPosition = " + " + string(startPosition/1000);
      end      
    
      labelTheAxes("", "", "Spec " + string(getNameFromSource(source)));  
    endfunction
    
    // Create a linear spectrogram
    // parameter [in] audio - audio file read in with wavread
    // parameter [in] specFreqSamples - number of frequencies
    // parameter [in] specTimeSamples - number of time samples
    // parameter [in] specOverlap - amount of overlap in spectrogram windows
    // parameter [in] hann - precalculated hanning window for smoothing
    // parameter [out] tempSpec - A matrix containing the spectrogram
    function tempSpec = createLinearSpectrogram( audio, specFreqSamples, specTimeSamples, specOverlap, hann )
    
      //Allocate memory for the input and output arrays. 
      tempSpec = zeros(St,specFreqSamples);
      in       = zeros(specTimeSamples);    //The input will have real values,
      out      = zeros(specFreqSamples,2);  //While the output will have complex values.It is also 1/2 the length of the input array +1.
    
      for i = 1:St
        //Store the input data into the input array
        for j = 1:specTimeSamples
          in(j) = audio(1,(i-1) * specTimeSamples/specOverlap + j);
          in(j) = in(j) * hann(j);
        end
    
        //Execute the FFT
        out(1:specTimeSamples,1) = real(fftw(in(1:specTimeSamples),1));
        out(1:specTimeSamples,2) = imag(fftw(in(1:specTimeSamples),1));
    
        //Convert the output into complex data and calculate the absolute value of every
        //output. Store it into the spectrogram array.
        for j = 1:specFreqSamples
          tempSpec(i,j) = sqrt(out(j,1) * out(j,1) + out(j,2) * out(j,2));
        end     
      end
    endfunction
    
    // Some utility functions
    function labelTheAxes(Title, LabelX, LabelY)
    
      a=get("current_axes");
      a.x_label;
      a.y_label;
      xtitle(string(Title),string(LabelX), string(LabelY)); 
    
    endfunction
    
    function numberOfSamples = msecToSamples( numberOfMilliSeconds, sampleRate)
      numberOfSamples   = round((numberOfMilliSeconds / 1000) * sampleRate);
    endfunction
    
    function croppedAudio = cropAudio(audio, startPosition, endPosition)
      //Parameters in Samples!!!
      croppedAudio = audio(1,startPosition:endPosition);
    endfunction