Results 1 to 4 of 4

Thread: Help with FFT

  1. #1
    Join Date
    Nov 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Help with FFT

    Hello,

    I'm using this class:

    Qt Code:
    1. #ifndef WAVE_H
    2. #define WAVE_H
    3.  
    4. #include <QAudioFormat>
    5. #include <QVector>
    6.  
    7. namespace ffft
    8. {
    9. template <typename T>
    10. class FFTReal;
    11. }
    12.  
    13. class Wave {
    14.  
    15. private:
    16. QVector<unsigned char> buffer;
    17. QAudioFormat format;
    18. int dirtyBegin, dirtyEnd; // dirty = modified with respect to parent
    19. Wave* parent;
    20. QString name;
    21. int disposed;
    22.  
    23. ffft::FFTReal<float>* fftreal;
    24. QList<QVector<float> > fftChunks;
    25.  
    26. // Helper functions for get/set left/right
    27. short getLeftRight (int index, bool left) const;
    28.  
    29.  
    30. void initfft();
    31. QVector<float> do_fft(QVector<float> input);
    32. QVector<float> do_ifft(QVector<float> input);
    33.  
    34. QVector<float> fftChunkLeftRight(int i);
    35. void ifftChunkLeftRight(int i, QVector<float> input);
    36. void setLeftRight (int index, short value, bool left);
    37.  
    38. public:
    39. // Ctor
    40. Wave(QString name, const QVector<unsigned char>& buffer, const QAudioFormat& format);
    41.  
    42. // Copy ctor
    43. explicit Wave(const Wave& wave);
    44.  
    45. // Name
    46. QString getName() const { return name; }
    47. void setName(QString name) { this->name=name; }
    48.  
    49. // High level API
    50. int sampleCount() const;
    51. void stretch(int samples);
    52. int frequency() const;
    53. int channels() const;
    54.  
    55. short getLeft(int index) const { return getLeftRight(index,true); }
    56. short getRight(int index) const { return getLeftRight(index,false); }
    57. void setLeft(int index, short value) { setLeftRight(index,value,true); }
    58. void setRight(int index, short value);
    59.  
    60. // Low level API
    61. const QAudioFormat& getFormat() const { return format; }
    62. const QVector<unsigned char>& getRealBuffer() const { return buffer; }
    63. const QVector<unsigned char>& getBuffer();
    64. void setFormat(const QAudioFormat& format);
    65. void setBuffer(const QVector<unsigned char>& buffer);
    66.  
    67. // FFT operations
    68. static const int fftChunkSize;
    69. int countFftChunks() const { return sampleCount() / fftChunkSize + 1; }
    70.  
    71. QVector<float> fftChunkLeft(int i) { return fftChunkLeftRight(i*2); }
    72. QVector<float> fftChunkRight(int i) { return fftChunkLeftRight(i*2+1); }
    73.  
    74. void ifftChunkLeft(int i, QVector<float> input) { ifftChunkLeftRight(i*2, input); }
    75. void ifftChunkRight(int i, QVector<float> input) { ifftChunkLeftRight(i*2+1, input); }
    76.  
    77. friend class WaveManager;
    78. };
    79.  
    80.  
    81. #endif // WAVE_H
    To copy to clipboard, switch view to plain text mode 


    I want to use this function "QVector<float> do_fft(QVector<float> input);" but have no idea, where/how to get this input vector of floats. And if someone can explain what represents this input vector. Thanks in advance

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Help with FFT

    You cannot directly use that function from outside because it is private to the class.

    The input to an FFT is a time-series of floating point samples of the waveform you are trying to analyse. The output is the equivalent of that series into the frequency domain. Where you get the input from is entirely driven by what you are trying to analyse. If you have a digital audio stream then the FFT input samples are the samples in the stream (converted to floats as required). If you have a weak analogue signal from a radio telescope then you need to apply analogue-to-digital conversion to obtain a suitable stream. You can fake a set of input data for testing fairly easily.

  3. #3
    Join Date
    Nov 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with FFT

    Thanks for the reply.
    Now I got the output from the fft and the output is QVector<float> within range [-1, 1]. I can calculate the amplitude like this:
    qreal magnitude = sqrt(real*real + imag*imag);
    qreal amplitude = 0.15 * log(magnitude);
    But I cannot find the way to calculate the frequencies from this vector. Any help about this is welcome

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Help with FFT

    The frequency starts at zero and has increment 1/(input sample delta). That is, if your inputs have a sample rate of 10, then the frequency increment is 1/10. There might be a factor of 2 PI in there somewhere as well. Depends on how this library was written.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.