Experimental implementation of the adaptive filter of "Interface" magazine in 2016-2017
Dependencies: amakusa mbed-dsp mbed shimabara ukifune unzen_nucleo_f746
Fork of skeleton_unzen_nucleo_f746 by
ハードウェアおよびソフトウェアはskelton_unzen_nucleo_f746を基本にしています。
vfo.cpp@18:b9b1116f8768, 2017-02-01 (annotated)
- Committer:
- shorie
- Date:
- Wed Feb 01 15:00:31 2017 +0000
- Revision:
- 18:b9b1116f8768
- Parent:
- 17:728ffc633179
Update ciomment
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shorie | 14:cec63d8da48c | 1 | #include "signal_processing.h" |
shorie | 14:cec63d8da48c | 2 | |
shorie | 18:b9b1116f8768 | 3 | |
shorie | 14:cec63d8da48c | 4 | VFO::VFO( void ) |
shorie | 14:cec63d8da48c | 5 | { |
shorie | 14:cec63d8da48c | 6 | // initial parameter setting. |
shorie | 18:b9b1116f8768 | 7 | this->form = triangle; |
shorie | 14:cec63d8da48c | 8 | this->Fs = 48000; |
shorie | 14:cec63d8da48c | 9 | this->frequency = 440; |
shorie | 14:cec63d8da48c | 10 | this->duty_cycle = 0.5; |
shorie | 16:d4ea3e6a0bce | 11 | |
shorie | 16:d4ea3e6a0bce | 12 | this->update_parameters(); |
shorie | 14:cec63d8da48c | 13 | } // End of constructor() |
shorie | 18:b9b1116f8768 | 14 | |
shorie | 18:b9b1116f8768 | 15 | VFO::~VFO( void ) |
shorie | 18:b9b1116f8768 | 16 | { |
shorie | 18:b9b1116f8768 | 17 | // do nothing |
shorie | 18:b9b1116f8768 | 18 | } |
shorie | 14:cec63d8da48c | 19 | |
shorie | 14:cec63d8da48c | 20 | |
shorie | 18:b9b1116f8768 | 21 | |
shorie | 14:cec63d8da48c | 22 | void VFO::run( |
shorie | 14:cec63d8da48c | 23 | float out_buffer[], // vfo output buffer |
shorie | 14:cec63d8da48c | 24 | unsigned int block_size // block size [sample] |
shorie | 14:cec63d8da48c | 25 | ) |
shorie | 14:cec63d8da48c | 26 | { |
shorie | 14:cec63d8da48c | 27 | // place the signal processing coce here |
shorie | 14:cec63d8da48c | 28 | for ( int i= 0; i< block_size; i++ ) |
shorie | 14:cec63d8da48c | 29 | { |
shorie | 15:de22b9d147e0 | 30 | // 1 : if phase < half_way; 0 : others. |
shorie | 18:b9b1116f8768 | 31 | if ( this->form == square ) |
shorie | 15:de22b9d147e0 | 32 | { |
shorie | 15:de22b9d147e0 | 33 | if ( this->current_phase < this->half_way ) |
shorie | 16:d4ea3e6a0bce | 34 | out_buffer[i] = 1.0; |
shorie | 15:de22b9d147e0 | 35 | else |
shorie | 15:de22b9d147e0 | 36 | out_buffer[i] = 0.0; |
shorie | 15:de22b9d147e0 | 37 | } |
shorie | 18:b9b1116f8768 | 38 | else // form == triangle |
shorie | 16:d4ea3e6a0bce | 39 | { |
shorie | 16:d4ea3e6a0bce | 40 | if ( this->current_phase < this->half_way ) |
shorie | 16:d4ea3e6a0bce | 41 | out_buffer[i] = this->rising_rate * this->current_phase; |
shorie | 16:d4ea3e6a0bce | 42 | else |
shorie | 16:d4ea3e6a0bce | 43 | out_buffer[i] = 1 + this->falling_rate * ( this->current_phase - this->half_way ); |
shorie | 16:d4ea3e6a0bce | 44 | } |
shorie | 15:de22b9d147e0 | 45 | |
shorie | 15:de22b9d147e0 | 46 | // update phase |
shorie | 15:de22b9d147e0 | 47 | this->current_phase += this->frequency; |
shorie | 15:de22b9d147e0 | 48 | // limit the range of the phase. |
shorie | 15:de22b9d147e0 | 49 | if ( this->current_phase >= this->Fs ) |
shorie | 15:de22b9d147e0 | 50 | this->current_phase -= this->Fs; |
shorie | 14:cec63d8da48c | 51 | } |
shorie | 14:cec63d8da48c | 52 | } // End of run() |
shorie | 14:cec63d8da48c | 53 | |
shorie | 14:cec63d8da48c | 54 | |
shorie | 14:cec63d8da48c | 55 | void VFO::set_Fs( int Fs ) |
shorie | 14:cec63d8da48c | 56 | { |
shorie | 14:cec63d8da48c | 57 | // regulate the Fs. |
shorie | 14:cec63d8da48c | 58 | if ( Fs != 32000 && Fs != 44100 && Fs != 96000 && Fs != 48000 ) |
shorie | 14:cec63d8da48c | 59 | Fs = 48000; |
shorie | 14:cec63d8da48c | 60 | this->Fs = Fs; |
shorie | 16:d4ea3e6a0bce | 61 | |
shorie | 16:d4ea3e6a0bce | 62 | this->update_parameters(); |
shorie | 14:cec63d8da48c | 63 | } |
shorie | 14:cec63d8da48c | 64 | |
shorie | 14:cec63d8da48c | 65 | void VFO::set_frequency( int freq ) |
shorie | 14:cec63d8da48c | 66 | { |
shorie | 14:cec63d8da48c | 67 | if ( freq > this->Fs / 4 ) |
shorie | 16:d4ea3e6a0bce | 68 | freq = Fs / 4; |
shorie | 14:cec63d8da48c | 69 | this->frequency = freq; |
shorie | 16:d4ea3e6a0bce | 70 | |
shorie | 16:d4ea3e6a0bce | 71 | this->update_parameters(); |
shorie | 14:cec63d8da48c | 72 | } |
shorie | 14:cec63d8da48c | 73 | |
shorie | 14:cec63d8da48c | 74 | void VFO::set_duty_cycle( float duty ) |
shorie | 14:cec63d8da48c | 75 | { |
shorie | 14:cec63d8da48c | 76 | if ( duty > 0.5f ) // high limit |
shorie | 14:cec63d8da48c | 77 | duty = 0.5f; |
shorie | 17:728ffc633179 | 78 | if ( duty < 0.0f ) // low limit |
shorie | 17:728ffc633179 | 79 | duty = 0.0f; |
shorie | 14:cec63d8da48c | 80 | this->duty_cycle = duty; |
shorie | 16:d4ea3e6a0bce | 81 | |
shorie | 16:d4ea3e6a0bce | 82 | this->update_parameters(); |
shorie | 14:cec63d8da48c | 83 | } |
shorie | 14:cec63d8da48c | 84 | |
shorie | 18:b9b1116f8768 | 85 | void VFO::set_wave_form( wave_form form ) |
shorie | 14:cec63d8da48c | 86 | { |
shorie | 18:b9b1116f8768 | 87 | this->form = form; |
shorie | 14:cec63d8da48c | 88 | } |
shorie | 14:cec63d8da48c | 89 | |
shorie | 14:cec63d8da48c | 90 | |
shorie | 16:d4ea3e6a0bce | 91 | // update the internal parameter by given parameters |
shorie | 16:d4ea3e6a0bce | 92 | void VFO::update_parameters(void) |
shorie | 16:d4ea3e6a0bce | 93 | { |
shorie | 16:d4ea3e6a0bce | 94 | // calc the half_way; |
shorie | 17:728ffc633179 | 95 | this-> half_way = this->Fs * this-> duty_cycle; |
shorie | 16:d4ea3e6a0bce | 96 | |
shorie | 16:d4ea3e6a0bce | 97 | // forbid to be zero. |
shorie | 16:d4ea3e6a0bce | 98 | if ( this-> half_way < this->frequency ) |
shorie | 16:d4ea3e6a0bce | 99 | half_way = this->frequency; |
shorie | 16:d4ea3e6a0bce | 100 | |
shorie | 16:d4ea3e6a0bce | 101 | // for triangle wave; |
shorie | 16:d4ea3e6a0bce | 102 | this->rising_rate = 1.0 / this->half_way; |
shorie | 16:d4ea3e6a0bce | 103 | |
shorie | 16:d4ea3e6a0bce | 104 | this->falling_rate = - 1.0 / ( this->Fs - this->half_way ); |
shorie | 16:d4ea3e6a0bce | 105 | } |
shorie | 16:d4ea3e6a0bce | 106 |