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 seiichi horie

ハードウェアおよびソフトウェアはskelton_unzen_nucleo_f746を基本にしています。

Committer:
shorie
Date:
Tue Jan 31 14:19:16 2017 +0000
Revision:
17:728ffc633179
Parent:
16:d4ea3e6a0bce
Child:
18:b9b1116f8768
VFO is now working.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shorie 14:cec63d8da48c 1 #include "signal_processing.h"
shorie 14:cec63d8da48c 2
shorie 14:cec63d8da48c 3 // Modify this constructor to initialize your audio algorithm.
shorie 14:cec63d8da48c 4 VFO::VFO( void )
shorie 14:cec63d8da48c 5 {
shorie 14:cec63d8da48c 6 // initial parameter setting.
shorie 14:cec63d8da48c 7 this->style = 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 14:cec63d8da48c 14
shorie 14:cec63d8da48c 15
shorie 14:cec63d8da48c 16 // Modify this method to implement your audio algorithm.
shorie 14:cec63d8da48c 17 void VFO::run(
shorie 14:cec63d8da48c 18 float out_buffer[], // vfo output buffer
shorie 14:cec63d8da48c 19 unsigned int block_size // block size [sample]
shorie 14:cec63d8da48c 20 )
shorie 14:cec63d8da48c 21 {
shorie 14:cec63d8da48c 22 // place the signal processing coce here
shorie 14:cec63d8da48c 23 for ( int i= 0; i< block_size; i++ )
shorie 14:cec63d8da48c 24 {
shorie 15:de22b9d147e0 25 // 1 : if phase < half_way; 0 : others.
shorie 15:de22b9d147e0 26 if ( this->style == square )
shorie 15:de22b9d147e0 27 {
shorie 15:de22b9d147e0 28 if ( this->current_phase < this->half_way )
shorie 16:d4ea3e6a0bce 29 out_buffer[i] = 1.0;
shorie 15:de22b9d147e0 30 else
shorie 15:de22b9d147e0 31 out_buffer[i] = 0.0;
shorie 15:de22b9d147e0 32 }
shorie 16:d4ea3e6a0bce 33 else // style == triangle
shorie 16:d4ea3e6a0bce 34 {
shorie 16:d4ea3e6a0bce 35 if ( this->current_phase < this->half_way )
shorie 16:d4ea3e6a0bce 36 out_buffer[i] = this->rising_rate * this->current_phase;
shorie 16:d4ea3e6a0bce 37 else
shorie 16:d4ea3e6a0bce 38 out_buffer[i] = 1 + this->falling_rate * ( this->current_phase - this->half_way );
shorie 16:d4ea3e6a0bce 39 }
shorie 15:de22b9d147e0 40
shorie 15:de22b9d147e0 41 // update phase
shorie 15:de22b9d147e0 42 this->current_phase += this->frequency;
shorie 15:de22b9d147e0 43 // limit the range of the phase.
shorie 15:de22b9d147e0 44 if ( this->current_phase >= this->Fs )
shorie 15:de22b9d147e0 45 this->current_phase -= this->Fs;
shorie 14:cec63d8da48c 46 }
shorie 14:cec63d8da48c 47 } // End of run()
shorie 14:cec63d8da48c 48
shorie 14:cec63d8da48c 49
shorie 14:cec63d8da48c 50 void VFO::set_Fs( int Fs )
shorie 14:cec63d8da48c 51 {
shorie 14:cec63d8da48c 52 // regulate the Fs.
shorie 14:cec63d8da48c 53 if ( Fs != 32000 && Fs != 44100 && Fs != 96000 && Fs != 48000 )
shorie 14:cec63d8da48c 54 Fs = 48000;
shorie 14:cec63d8da48c 55 this->Fs = Fs;
shorie 16:d4ea3e6a0bce 56
shorie 16:d4ea3e6a0bce 57 this->update_parameters();
shorie 14:cec63d8da48c 58 }
shorie 14:cec63d8da48c 59
shorie 14:cec63d8da48c 60 void VFO::set_frequency( int freq )
shorie 14:cec63d8da48c 61 {
shorie 14:cec63d8da48c 62 if ( freq > this->Fs / 4 )
shorie 16:d4ea3e6a0bce 63 freq = Fs / 4;
shorie 14:cec63d8da48c 64 this->frequency = freq;
shorie 16:d4ea3e6a0bce 65
shorie 16:d4ea3e6a0bce 66 this->update_parameters();
shorie 14:cec63d8da48c 67 }
shorie 14:cec63d8da48c 68
shorie 14:cec63d8da48c 69 void VFO::set_duty_cycle( float duty )
shorie 14:cec63d8da48c 70 {
shorie 14:cec63d8da48c 71 if ( duty > 0.5f ) // high limit
shorie 14:cec63d8da48c 72 duty = 0.5f;
shorie 17:728ffc633179 73 if ( duty < 0.0f ) // low limit
shorie 17:728ffc633179 74 duty = 0.0f;
shorie 14:cec63d8da48c 75 this->duty_cycle = duty;
shorie 16:d4ea3e6a0bce 76
shorie 16:d4ea3e6a0bce 77 this->update_parameters();
shorie 14:cec63d8da48c 78 }
shorie 14:cec63d8da48c 79
shorie 14:cec63d8da48c 80 void VFO::set_wave_style( wave_style style )
shorie 14:cec63d8da48c 81 {
shorie 14:cec63d8da48c 82 this->style = style;
shorie 14:cec63d8da48c 83 }
shorie 14:cec63d8da48c 84
shorie 14:cec63d8da48c 85
shorie 16:d4ea3e6a0bce 86 // update the internal parameter by given parameters
shorie 16:d4ea3e6a0bce 87 void VFO::update_parameters(void)
shorie 16:d4ea3e6a0bce 88 {
shorie 16:d4ea3e6a0bce 89 // calc the half_way;
shorie 17:728ffc633179 90 this-> half_way = this->Fs * this-> duty_cycle;
shorie 16:d4ea3e6a0bce 91
shorie 16:d4ea3e6a0bce 92 // forbid to be zero.
shorie 16:d4ea3e6a0bce 93 if ( this-> half_way < this->frequency )
shorie 16:d4ea3e6a0bce 94 half_way = this->frequency;
shorie 16:d4ea3e6a0bce 95
shorie 16:d4ea3e6a0bce 96 // for triangle wave;
shorie 16:d4ea3e6a0bce 97 this->rising_rate = 1.0 / this->half_way;
shorie 16:d4ea3e6a0bce 98
shorie 16:d4ea3e6a0bce 99 this->falling_rate = - 1.0 / ( this->Fs - this->half_way );
shorie 16:d4ea3e6a0bce 100 }
shorie 16:d4ea3e6a0bce 101