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@8:1fa224c83cfa, 2017-02-18 (annotated)
- Committer:
- shorie
- Date:
- Sat Feb 18 01:05:19 2017 +0000
- Revision:
- 8:1fa224c83cfa
- Parent:
- 2:fbdf03b330ae
Fixed mistake of visibility in the limitterlinatan.h
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 | 2:fbdf03b330ae | 10 | amakusa::OSCSinCos::OSCSinCos( float freq, int Fs, int block_size ) |
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 | 2:fbdf03b330ae | 15 | this->bs = block_size; |
shorie | 0:058daa59980d | 16 | } |
shorie | 0:058daa59980d | 17 | |
shorie | 2:fbdf03b330ae | 18 | void amakusa::OSCSinCos::run( float *s, float *c) |
shorie | 0:058daa59980d | 19 | { |
shorie | 2:fbdf03b330ae | 20 | |
shorie | 2:fbdf03b330ae | 21 | for ( int i=0; i<this->bs; i++ ) |
shorie | 0:058daa59980d | 22 | { |
shorie | 0:058daa59980d | 23 | // to make the expression simple |
shorie | 0:058daa59980d | 24 | float phase = this->deltaPhase * ( i+ this->phaseInSample ); |
shorie | 0:058daa59980d | 25 | |
shorie | 2:fbdf03b330ae | 26 | // normal sind cosine |
shorie | 0:058daa59980d | 27 | s[i] = std::sin( phase ); |
shorie | 0:058daa59980d | 28 | // skip cosine if c is NULL |
shorie | 0:058daa59980d | 29 | if ( c != NULL ) |
shorie | 0:058daa59980d | 30 | c[i] = std::cos( phase ); |
shorie | 2:fbdf03b330ae | 31 | |
shorie | 2:fbdf03b330ae | 32 | |
shorie | 0:058daa59980d | 33 | } |
shorie | 0:058daa59980d | 34 | // update the internal phase |
shorie | 2:fbdf03b330ae | 35 | this->phaseInSample += this->bs; |
shorie | 0:058daa59980d | 36 | // truncate the phase |
shorie | 0:058daa59980d | 37 | while ( this->phaseInSample >= this->sampleFreq ) |
shorie | 0:058daa59980d | 38 | this->phaseInSample -= this->sampleFreq; |
shorie | 0:058daa59980d | 39 | } |
shorie | 0:058daa59980d | 40 | |
shorie | 0:058daa59980d | 41 | |
shorie | 0:058daa59980d | 42 | void amakusa::OSCSinCos::setFrequency( float freq ) |
shorie | 0:058daa59980d | 43 | { |
shorie | 0:058daa59980d | 44 | // convert frequency to the update phase per sample. |
shorie | 0:058daa59980d | 45 | this->deltaPhase = 2 * M_PI * freq / this->sampleFreq; |
shorie | 0:058daa59980d | 46 | } |
shorie | 0:058daa59980d | 47 | |
shorie | 0:058daa59980d | 48 | // put the phase into the range [0, 2pi) |
shorie | 0:058daa59980d | 49 | void amakusa::OSCSinCos::setPhase( float phase ) |
shorie | 0:058daa59980d | 50 | { |
shorie | 0:058daa59980d | 51 | // convert phisical phase to the phase in sample. |
shorie | 0:058daa59980d | 52 | this->phaseInSample = phase * this->sampleFreq / ( 2 * M_PI ); |
shorie | 0:058daa59980d | 53 | |
shorie | 0:058daa59980d | 54 | // truncate phase in to [0,2pi) |
shorie | 0:058daa59980d | 55 | while ( this->phaseInSample < 0 ) |
shorie | 0:058daa59980d | 56 | this->phaseInSample += this->sampleFreq; |
shorie | 0:058daa59980d | 57 | while ( this->phaseInSample >= this->sampleFreq ) |
shorie | 0:058daa59980d | 58 | this->phaseInSample -= this->sampleFreq; |
shorie | 0:058daa59980d | 59 | } |
shorie | 0:058daa59980d | 60 | |
shorie | 0:058daa59980d | 61 | float amakusa::OSCSinCos::getFrequency() |
shorie | 0:058daa59980d | 62 | { |
shorie | 0:058daa59980d | 63 | // convert sample |
shorie | 0:058daa59980d | 64 | return this->deltaPhase * this->sampleFreq / ( 2 * M_PI ); |
shorie | 0:058daa59980d | 65 | } |
shorie | 0:058daa59980d | 66 | /** |
shorie | 0:058daa59980d | 67 | * \brief get the internal phase |
shorie | 0:058daa59980d | 68 | * \returns Phase in radian |
shorie | 0:058daa59980d | 69 | */ |
shorie | 0:058daa59980d | 70 | float amakusa::OSCSinCos::getPhase() |
shorie | 0:058daa59980d | 71 | { |
shorie | 0:058daa59980d | 72 | return this->phaseInSample * 2 * M_PI / this->sampleFreq; |
shorie | 0:058daa59980d | 73 | } |