Basic Audio Signal Processing Library
Dependents: unzen_sample_nucleo_f746 skeleton_unzen_nucleo_f746 ifmag_noise_canceller synthesizer_f746
オーディオ信号処理用のライブラリです。
mbed-dspのフィルタ群向けに作ったクラス・ラッパーのほか、以下のクラスを用意しています。
- ヒステリシス
- sin/cosオシレータ
- リミッター
クラスは全て名前空間amakusaに含まれます。
oscsincos.cpp@0:058daa59980d, 2016-12-11 (annotated)
- Committer:
- shorie
- Date:
- Sun Dec 11 21:04:06 2016 +0000
- Revision:
- 0:058daa59980d
- Child:
- 2:fbdf03b330ae
Publish
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shorie | 0:058daa59980d | 1 | #include <cstdlib> |
shorie | 0:058daa59980d | 2 | #include <cmath> |
shorie | 0:058daa59980d | 3 | |
shorie | 0:058daa59980d | 4 | #include "amakusa.h" |
shorie | 0:058daa59980d | 5 | |
shorie | 0:058daa59980d | 6 | #ifndef M_PI |
shorie | 0:058daa59980d | 7 | #define M_PI 3.141592653589793F |
shorie | 0:058daa59980d | 8 | #endif |
shorie | 0:058daa59980d | 9 | |
shorie | 0:058daa59980d | 10 | amakusa::OSCSinCos::OSCSinCos( float freq, int Fs ) |
shorie | 0:058daa59980d | 11 | { |
shorie | 0:058daa59980d | 12 | this->sampleFreq = Fs; |
shorie | 0:058daa59980d | 13 | this->setFrequency( freq ); |
shorie | 0:058daa59980d | 14 | this->setPhase( 0.0 ); // must call after setFrequenc() |
shorie | 0:058daa59980d | 15 | } |
shorie | 0:058daa59980d | 16 | |
shorie | 0:058daa59980d | 17 | void amakusa::OSCSinCos::run( float *s, float *c , int count ) |
shorie | 0:058daa59980d | 18 | { |
shorie | 0:058daa59980d | 19 | for ( int i=0; i<count; i++ ) |
shorie | 0:058daa59980d | 20 | { |
shorie | 0:058daa59980d | 21 | // to make the expression simple |
shorie | 0:058daa59980d | 22 | float phase = this->deltaPhase * ( i+ this->phaseInSample ); |
shorie | 0:058daa59980d | 23 | |
shorie | 0:058daa59980d | 24 | s[i] = std::sin( phase ); |
shorie | 0:058daa59980d | 25 | // skip cosine if c is NULL |
shorie | 0:058daa59980d | 26 | if ( c != NULL ) |
shorie | 0:058daa59980d | 27 | c[i] = std::cos( phase ); |
shorie | 0:058daa59980d | 28 | } |
shorie | 0:058daa59980d | 29 | // update the internal phase |
shorie | 0:058daa59980d | 30 | this->phaseInSample += count; |
shorie | 0:058daa59980d | 31 | // truncate the phase |
shorie | 0:058daa59980d | 32 | while ( this->phaseInSample >= this->sampleFreq ) |
shorie | 0:058daa59980d | 33 | this->phaseInSample -= this->sampleFreq; |
shorie | 0:058daa59980d | 34 | } |
shorie | 0:058daa59980d | 35 | |
shorie | 0:058daa59980d | 36 | |
shorie | 0:058daa59980d | 37 | void amakusa::OSCSinCos::setFrequency( float freq ) |
shorie | 0:058daa59980d | 38 | { |
shorie | 0:058daa59980d | 39 | // convert frequency to the update phase per sample. |
shorie | 0:058daa59980d | 40 | this->deltaPhase = 2 * M_PI * freq / this->sampleFreq; |
shorie | 0:058daa59980d | 41 | } |
shorie | 0:058daa59980d | 42 | |
shorie | 0:058daa59980d | 43 | // put the phase into the range [0, 2pi) |
shorie | 0:058daa59980d | 44 | void amakusa::OSCSinCos::setPhase( float phase ) |
shorie | 0:058daa59980d | 45 | { |
shorie | 0:058daa59980d | 46 | // convert phisical phase to the phase in sample. |
shorie | 0:058daa59980d | 47 | this->phaseInSample = phase * this->sampleFreq / ( 2 * M_PI ); |
shorie | 0:058daa59980d | 48 | |
shorie | 0:058daa59980d | 49 | // truncate phase in to [0,2pi) |
shorie | 0:058daa59980d | 50 | while ( this->phaseInSample < 0 ) |
shorie | 0:058daa59980d | 51 | this->phaseInSample += this->sampleFreq; |
shorie | 0:058daa59980d | 52 | while ( this->phaseInSample >= this->sampleFreq ) |
shorie | 0:058daa59980d | 53 | this->phaseInSample -= this->sampleFreq; |
shorie | 0:058daa59980d | 54 | } |
shorie | 0:058daa59980d | 55 | |
shorie | 0:058daa59980d | 56 | float amakusa::OSCSinCos::getFrequency() |
shorie | 0:058daa59980d | 57 | { |
shorie | 0:058daa59980d | 58 | // convert sample |
shorie | 0:058daa59980d | 59 | return this->deltaPhase * this->sampleFreq / ( 2 * M_PI ); |
shorie | 0:058daa59980d | 60 | } |
shorie | 0:058daa59980d | 61 | /** |
shorie | 0:058daa59980d | 62 | * \brief get the internal phase |
shorie | 0:058daa59980d | 63 | * \returns Phase in radian |
shorie | 0:058daa59980d | 64 | */ |
shorie | 0:058daa59980d | 65 | float amakusa::OSCSinCos::getPhase() |
shorie | 0:058daa59980d | 66 | { |
shorie | 0:058daa59980d | 67 | return this->phaseInSample * 2 * M_PI / this->sampleFreq; |
shorie | 0:058daa59980d | 68 | } |