Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8120425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:09:35+00:00 2026-06-06T05:09:35+00:00

I know it was asked a thousand times before, but I still can’t find

  • 0

I know it was asked a thousand times before, but I still can’t find a solution.
Searching SO, I indeed found the algorithm for it, but lacking the mathematical knowledge required to truly understand it, I am helplessly lost!

To start with the beginning, my goal is to compute an entire spectrogram and save it to an image in order to use it for a visualizer.

I tried using Sound.computeSpectrum, but this requires to play the sound and wait for it to end, I want to compute the spectrogram in a way shorter time than that will require to listen all the song. And I have 2 hours long mp3s.

What I am doing now is to read the bytes from a Sound object, the separate into two Vectors(.); Then using a timer, at each 100 ms I call a function (step1) where I have the implementation of the algorithm, as follows:

  1. for each vector (each for a channel) I apply the hann function to the elements;
  2. for each vector I nullify the imaginary part (I have a secondary vector for that)
  3. for each vector I apply FFT
  4. for each vector I find the magnitude for the first N / 2 elements
  5. for each vector I convert squared magnitude to dB scale
  6. end.

But I get only negative values, and only 30 percent of the results might be useful (in the way that the rest are identical)

I will post the code for only one channel to get rid off the “for each vector” part.

private var N:Number = 512;
private function step1() : void
{
    var xReLeft:Vector.<Number> = new Vector.<Number>(N);
    var xImLeft:Vector.<Number> = new Vector.<Number>(N);

    var leftA:Vector.<Number> = new Vector.<Number>(N);

    // getting sample range
    leftA = this.channels.left.slice(step * N, step * (N) + (N));

    if (leftA.length < N)
    {
        stepper.removeEventListener(TimerEvent.TIMER, getFreq100ms);
        return;
    }
    else if (leftA.length == 0)
    {
        stepper.removeEventListener(TimerEvent.TIMER, getFreq100ms);
        return;
    }

    var i:int;

    // hann window function init
        m_win = new Vector.<Number>(N);
        for ( var i:int = 0; i < N; i++ )
            m_win[i] = (4.0 / N) * 0.5 * (1 - Math.cos(2 * Math.PI * i / N));

    // applying hann window function
    for ( i = 0; i < N; i++ )
    {
        xReLeft[i] = m_win[i]*leftA[i];
        //xReRight[i] = m_win[i]*rightA[i];
    }

    // nullify the imaginary part
    for ( i = 0; i < N; i++ )
    {
        xImLeft[i] = 0.0;
        //xImRight[i] = 0.0;
    }

    var magnitutel:Vector.<Number> = new Vector.<Number>(N);

    fftl.run( xReLeft, xImLeft );

    current = xReLeft;
    currf = xImLeft;

    for ( i = 0; i < N / 2; i++ )
    {
        var re:Number = xReLeft[i];
        var im:Number = xImLeft[i];
        magnitutel[i] = Math.sqrt(re * re + im * im);
    }

    const SCALE:Number = 20 / Math.LN10;
    var l:uint = this.total.length;
    for ( i = 0; i < N / 2; i++ )
    {
        magnitutel[i] = SCALE * Math.log( magnitutel[i] + Number.MIN_VALUE );
    }

    var bufferl:Vector.<Number> = new Vector.<Number>();

    for (i = 0; i < N / 2 ; i++)
    {
        bufferl[i] =  magnitutel[i];
    }

    var complete:Vector.<Vector.<Number>> = new Vector.<Vector.<Number>>();
    complete[0] = bufferl;
    this.total[step] = complete;

    this.step++;
}

This function is executed in the event dispatched by the timer (stepper).
Obviously I do something wrong, as I said I have only negative values and further more values range between 1 and 7000 (at least).

I want to thank you in advance for any help.

With respect,
Paul

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T05:09:37+00:00Added an answer on June 6, 2026 at 5:09 am

    Negative dB values are OK. Just add a constant (representing your volume control) until the number of points you want to color become positive. The remaining values that stay negative are usually just displayed or colored as black in a spectrogram. No matter how negative (as they might just be the FFT’s numerical noise, which can be a huge negative dB number or even NaN or -Inf for log(0)).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this might have been asked a thousand times before, but I can't
I know this has been asked at least a thousand times but I can't
I know this has been asked thousands of times but I just can't find
I know this question was asked here many times before but I am still
Ok, I know this has been asked a thousand times before, but no conclusive
I know this has been asked a thousand times but I'm still having trouble
I know this has been asked probably a thousand times, but I've been biting
I know people asked this before and I found this solution: for (UIView *subview
Bear with me, I know access_token questions have been asked a thousand times but
Well I know this question is asked a thousand times on internet but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.