Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

c++ - drawing waveform - converting to DB squashes it

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...