Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting waveform of the .wav file

Tags:

c++

c

I wanted to plot the wave-form of the .wav file for the specific plotting width.

Which method should I use to display correct waveform plot ?

Any Suggestions , tutorial , links are welcomed....

like image 667
Ashish Avatar asked Jan 14 '10 17:01

Ashish


2 Answers

Basic algorithm:

  1. Find number of samples to fit into draw-window
  2. Determine how many samples should be presented by each pixel
  3. Calculate RMS (or peak) value for each pixel from a sample block. Averaging does not work for audio signals.
  4. Draw the values.

Let's assume that n(number of samples)=44100, w(width)=100 pixels:

then each pixel should represent 44100/100 == 441 samples (blocksize)

for (x = 0; x < w; x++)
    draw_pixel(x_offset + x,
               y_baseline - rms(&mono_samples[x * blocksize], blocksize));

Stuff to try for different visual appear:

  • rms vs max value from block
  • overlapping blocks (blocksize x but advance x/2 for each pixel etc)

Downsampling would not probably work as you would lose peak information.

like image 139
kauppi Avatar answered Oct 15 '22 18:10

kauppi


Either use RMS, BlockSize depends on how far you are zoomed in!

float RMS = 0;
for (int a = 0; a < BlockSize; a++)
{
    RMS += Samples[a]*Samples[a];
}
RMS = sqrt(RMS/BlockSize);

or Min/Max (this is what cool edit/Audtion Uses)

float Max = -10000000;
float Min = 1000000;
for (int a = 0; a < BlockSize; a++)
{
    if (Samples[a] > Max) Max = Samples[a];
    if (Samples[a] < Min) Min = Samples[a];
}
like image 38
shabtronic Avatar answered Oct 15 '22 20:10

shabtronic