Example #1
0
void HRV1MainModule::evaluateFFT(){
    alglib::real_1d_array fftTmp;
    QVector<double> fftModules;
    QVector<double> fftFrequences;
    double maxModuleValue;

	for(int i = 1; i < peaks.at(peaks.size() - 1); i = i + (int)(PRECISION*1000))
	{
        toReturnFrequency.rrYData->push_back(alglib::spline1dcalc(splineInterpolant, i));
        toReturnFrequency.rrXData->push_back(i);
	}



    fftTmp.setlength(toReturnFrequency.rrYData->size());

    for(int i = 0; i < toReturnFrequency.rrYData->size(); i++){
        fftTmp(i) = toReturnFrequency.rrYData->at(i);
	}

	alglib::fftr1d(fftTmp, fftArray);

    //fftFrequences.push_back(0);
    //fftModules.push_back(sqrt(pow(fftArray(0).x, 2) + pow(fftArray(0).y, 2))*2/fftArray.length());
	for(int i = 1; i < fftArray.length(); i++){
        fftFrequences.push_back((i)*(1/PRECISION)/(fftArray.length()));
        fftModules.push_back(sqrt(pow(fftArray(i).x, 2) + pow(fftArray(i).y, 2))*2/fftArray.length());
	}

    for(int i = 0; fftFrequences.at(i) <= FREQUENCY_TRESHOLD; i++)
    {
        if(fftModules.at(i) > maxModuleValue)
        {
            maxModuleValue = fftModules.at(i);
        }
    }

    for(int i = 0; fftFrequences.at(i) <= FREQUENCY_TRESHOLD; i++)
    {
        toReturnFrequency.xData->push_back(fftFrequences.at(i));
        toReturnFrequency.yData->push_back(pow(fftModules.at(i),2));
    }
}
Example #2
0
void Spectrum::update(const Sint16* stream, const int length, const float delta) {

  if (!_initialized || length == 0)
    return;

  int w = _image.getWidth();
  int h = _image.getHeight();
  int step = length / w;

  if (_timer.hasEnded()) {

    /* FFT */

    for (int i = 0; i < w; ++i) {
      float norm_amp = stream[i * step] * (1.f/32767.f);
      _rawValues[i] = Complex(norm_amp, 0);
    }

    std::valarray<Complex> fftArray(_rawValues, w);
    audio::FFT::fft(fftArray);

    for (int i = 0; i < w/2; ++i) {
      _fftValues[i] = std::log10(std::abs(fftArray[i])) * 20;
    }

    float octaves = calculateOctave(_minFreq, _maxFreq, _barCount);
    unsigned int currentFreq = _minFreq;
    for (unsigned int i = 0; i < _barCount; ++i) {

      int low = lowerLimitSample(currentFreq, octaves, w/2);
      int hi = upperLimitSample(currentFreq, octaves, w/2);

      if (currentFreq > Core::Audio()->getFrequencyRate() || hi > w/2)
        break;

      // Log.Info("C: %d [L: %d / H: %d]", currentFreq, low, hi);

      _barFrequencies[i] = currentFreq;

      _barValues[i] = averageFreq(_fftValues, low, hi);
      currentFreq = nextCenterFreq(currentFreq, octaves);
    }

    /* END FFT */

    _image.setRect(0, 0, w, h, Color::Black);
    drawBorders();

    int bar_size = w / (float)_barCount;
    for (unsigned int i = 0; i < _barCount; ++i) {

      float scale_val = std::max(0.f, _barValues[i]);
      float norm_val = scale_val / 30.f;

      _normalizedBarValues[i] = norm_val;

      Color color = math::interpolate(Color(255, 64, 0), Color::Red, norm_val, math::Interpolation::Linear);

      // Values
      // Log.Info("Bar %d: %f", i, val);

      int y = h - norm_val * h;
      _image.setRect(i*bar_size, y + 1, bar_size, h - y - 2, color);
    }

    _image.updateTextureZone(0, 0, w, h);
    _timer.reset();
  }

  _timer.update(delta);
}