1
votes

I have a wave file, i have a function that retrieves 2 samples per pixel then i draw lines with them. quick and painless before i deal with zooming. i can display the amplitude values no problem

enter image description here

that is an accurate image of the waveform. to do this i used the following code

//tempAllChannels[numOfSamples] holds amplitude data for the entire wav
//oneChannel[numOfPixels*2] will hold 2 values per pixel in display area, an average of min amp, and average of max
for(int i = 0; i < numOfSamples; i++)//loop through all samples in wave file
{
    if (tempAllChannels[i] < 0) min += tempAllChannels[i];//if neg amp value, add amp value to min
    if (tempAllChannels[i] >= 0) max += tempAllChannels[i]; 

    if(i%factor==0 && i!=0)     //factor is (numofsamples in wav)/(numofpixels) in display area
    {
        min = min/factor;       //get average amp value
        max = max/factor;
        oneChannel[j]=max;
        oneChannel[j+1]=min;
        j+=2;                   //iterate for next time
        min = 0;                //reset for next time
        max = 0;
    }   
}

and that's great but I need to display in db so quieter wave images arent ridiculously small, but when i make the following change to the above code

oneChannel[j]=10*log10(max);
oneChannel[j+1]=-10*log10(-min);

the wave image looks like this.

enter image description here

which isnt accurate, it looks like its being squashed. Is there something wrong with what I'm doing? I need to find a way to convert from amplitude to decibels whilst maintaining dynamics. im thinking i shouldnt be taking an average when converted to DB.

1
When you expand the small values, you squash the large ones. That's the only way to keep the same interval. It's a natural outcome of the math.Mark Ransom
i thought that may be the case. when i look at waveforms in audacity however, their waveforms are more equal. i had assumed they were displaying in decibels but they arent. I think there must be an issue with how im populating the array of average amplitudes. either that or they have figured out a way of slightly normalising to make things more readable by humans.cool mr croc

1 Answers

3
votes

Don't convert to dB for overviews. No one does that.

Instead of finding the average over a block, you should find the max of the absolute value. By averaging, you will loose a lot of amplitude in your high frequency peaks.