Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QVector always push back 0?
QtWS25 Last Chance

QVector always push back 0?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 710 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Yeoman_Li
    wrote on last edited by
    #1

    I am using FFTW to build a music visualizer. And in my header file, I construct a struct:

    struct SpectrumStruct{
        qreal frequency;
        qreal amplitude;
    };
    
    QVector<SpectrumStruct> m_spectrum;
    

    And in my .cpp file,

    void Widget::debug(QAudioBuffer buf)
    {
        QAudioBuffer :: S16S * data = buf.data<QAudioBuffer :: S16S>();
        int sampleRate = buf.format().sampleRate();
    //    int N = buf.frameCount();
        int N = 1024;
        fftw_complex *in, *out;
        in = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * N);
        out = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * N);
        for (int i = 0; i < N; ++i) {
            qreal hannwindow = 0.5 * (1 - qCos((2 * M_PI * i) / (N - 1)));
            in[i][0] = data[i].left * hannwindow;
            in[i][1] = 0;
        }
        fftw_plan myPlan = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
        fftw_execute(myPlan);
        for(int i = 0; i < N / 2; ++i) {
            struct SpectrumStruct thisSP;
            thisSP.frequency = double(i * sampleRate / N);
            qreal thisAmpt = qSqrt(out[i][0] * out[i][0] + out[i][1] * out[i][1]);
    //        thisAmpt = 2.0 * thisAmpt / N;
            thisAmpt = 0.15 * log10(thisAmpt);
            thisAmpt = qMax(qreal(0.0), thisAmpt);
            thisAmpt = qMin(qreal(1.0), thisAmpt);
            thisSP.amplitude = thisAmpt;
            m_spectrum.push_back(thisSP);
            qDebug() << m_spectrum[i].frequency << "\t" << m_spectrum[i].amplitude << "\t" << thisAmpt;
        }
    
        fftw_destroy_plan(myPlan);
        fftw_free(in);
        fftw_free(out);
    
    }
    

    I tried to print each sample point's frequency and amplitude . The thisAmpt should be same with the m_spectrum[i].amplitude. But finally in the console thisAmpt is correct but m_spectrum[i].amplitude is always 0. How can it be?

    Part of the console result:

    20887 0 0.0475125
    20930 0 0.0734866
    20973 0 0.0784833
    21016 0 0.156529
    21059 0 0.19284
    21102 0 0.168585
    21145 0 0.134795
    21188 0 0.119863
    21231 0 0.122281
    21274 0 0.138717
    21317 0 0.15457
    21360 0 0.139525
    21404 0 0.0697819
    21447 0 0.0985039
    21490 0 0.153734
    21533 0 0.147471
    21576 0 0.0658756
    21619 0 0.13765
    21662 0 0.151573
    21705 0 0.179327
    21748 0 0.184664
    21791 0 0.162867
    21834 0 0.166042
    21877 0 0.128155
    21920 0 0.0839292
    21963 0 0.0618584
    22006 0 0.146992

    J.HilkJ 1 Reply Last reply
    0
    • Y Yeoman_Li

      I am using FFTW to build a music visualizer. And in my header file, I construct a struct:

      struct SpectrumStruct{
          qreal frequency;
          qreal amplitude;
      };
      
      QVector<SpectrumStruct> m_spectrum;
      

      And in my .cpp file,

      void Widget::debug(QAudioBuffer buf)
      {
          QAudioBuffer :: S16S * data = buf.data<QAudioBuffer :: S16S>();
          int sampleRate = buf.format().sampleRate();
      //    int N = buf.frameCount();
          int N = 1024;
          fftw_complex *in, *out;
          in = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * N);
          out = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * N);
          for (int i = 0; i < N; ++i) {
              qreal hannwindow = 0.5 * (1 - qCos((2 * M_PI * i) / (N - 1)));
              in[i][0] = data[i].left * hannwindow;
              in[i][1] = 0;
          }
          fftw_plan myPlan = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
          fftw_execute(myPlan);
          for(int i = 0; i < N / 2; ++i) {
              struct SpectrumStruct thisSP;
              thisSP.frequency = double(i * sampleRate / N);
              qreal thisAmpt = qSqrt(out[i][0] * out[i][0] + out[i][1] * out[i][1]);
      //        thisAmpt = 2.0 * thisAmpt / N;
              thisAmpt = 0.15 * log10(thisAmpt);
              thisAmpt = qMax(qreal(0.0), thisAmpt);
              thisAmpt = qMin(qreal(1.0), thisAmpt);
              thisSP.amplitude = thisAmpt;
              m_spectrum.push_back(thisSP);
              qDebug() << m_spectrum[i].frequency << "\t" << m_spectrum[i].amplitude << "\t" << thisAmpt;
          }
      
          fftw_destroy_plan(myPlan);
          fftw_free(in);
          fftw_free(out);
      
      }
      

      I tried to print each sample point's frequency and amplitude . The thisAmpt should be same with the m_spectrum[i].amplitude. But finally in the console thisAmpt is correct but m_spectrum[i].amplitude is always 0. How can it be?

      Part of the console result:

      20887 0 0.0475125
      20930 0 0.0734866
      20973 0 0.0784833
      21016 0 0.156529
      21059 0 0.19284
      21102 0 0.168585
      21145 0 0.134795
      21188 0 0.119863
      21231 0 0.122281
      21274 0 0.138717
      21317 0 0.15457
      21360 0 0.139525
      21404 0 0.0697819
      21447 0 0.0985039
      21490 0 0.153734
      21533 0 0.147471
      21576 0 0.0658756
      21619 0 0.13765
      21662 0 0.151573
      21705 0 0.179327
      21748 0 0.184664
      21791 0 0.162867
      21834 0 0.166042
      21877 0 0.128155
      21920 0 0.0839292
      21963 0 0.0618584
      22006 0 0.146992

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @Yeoman_Li
      mmh, to be honest, I would expect m_spectrum[i].amplitude and thisAmpt to be the same value. Does this also happen, when you use append instead of push_back ? And what Qt-Version is this ?


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      Y 1 Reply Last reply
      1
      • E Offline
        E Offline
        eliseev
        wrote on last edited by
        #3

        I don't see m_spectrum cleared in this function. Without it m_spectrum[i] is not necessarily equal to thisSP. Try m_spectrum.last().

        Y 1 Reply Last reply
        3
        • J.HilkJ J.Hilk

          @Yeoman_Li
          mmh, to be honest, I would expect m_spectrum[i].amplitude and thisAmpt to be the same value. Does this also happen, when you use append instead of push_back ? And what Qt-Version is this ?

          Y Offline
          Y Offline
          Yeoman_Li
          wrote on last edited by
          #4

          @J.Hilk I just make a silly mistake. Forget to clear the vector before add value to it.
          See https://stackoverflow.com/questions/53833570/qvector-always-push-back-0/53834747#53834747

          1 Reply Last reply
          2
          • E eliseev

            I don't see m_spectrum cleared in this function. Without it m_spectrum[i] is not necessarily equal to thisSP. Try m_spectrum.last().

            Y Offline
            Y Offline
            Yeoman_Li
            wrote on last edited by
            #5

            @eliseev You are right, I forget to clear it.Thank you:)

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved