-1

I'm working on project, where I need to visualize spectral analysis to set some precise parameters. Now I'm with conversion of bins to screen space, because in linear space, magnitudes in lower frequencies are squashed together. Here's my code in c++:

float windowSize = 640;
float windowHeight = 480;
for (size_t i = 0; i < bins; i++)
{
    float m = audioIn.getSpectrum.at(i)*windowHeight;
    float pos = i;
    drawLine(vec2(pos, 0), vec2(pos, m));
}

I was trying to compute pos by using different approaches, but failed miserably. I'm missing crucial knowledge about logarithms I guess.

DISCLAIMER: this is for personal art project, not homework assignment.

sphere42
  • 156
  • 1
  • 11

1 Answers1

2

Typically spectrographs are displayed on a base 10 logarithmic scale.

Assuming bins in your case go from 0 Hz to nyquist Hz you might try something like this (for 44.1kHz audio):

float nyquist = 22050.0;
float logMax = log10(nyquist);
float log = log10((float)i * nyquist / (float)bins);
float pos = log / logMax * windowSize;
Tim
  • 4,560
  • 2
  • 40
  • 64