pulse_wave.hpp:
#include<iostream> #include<vector> #include<stdio.h> static const int wave_table_len=50; class pulse_wave_class{ public: pulse_wave_class(const float tau): _wave_table(wave_table_len) { // std::vector<float>wave_table(wave_table_len); for(int i=0;i<wave_table_len;i++) _wave_table[i]=(i<tau*wave_table_len)?1:0; } inline float operator()(const int index)const{ return _wave_table[index % wave_table_len]; } void print() { for(int j=0;j<wave_table_len;j++) printf("%f\t",_wave_table[j]); printf("\n"); } //private: std::vector <float> _wave_table; };
pulse_tb.cpp:
using namespace std; #include"pulse_wave.hpp" #include<iostream> #include<stdio.h> int main() { pulse_wave_class wave_table(0.5); int i; //wave_table.print(); for(i=0;i<100;i++) //printf("%f\t",wave_table._wave_table[i]); //printf("\n"); if(wave_table._wave_table[i%50]>0.5) printf("sending...\n"); else printf("receiving...\n"); return 0; }
编译:g++ pulse_tb.cpp pulse_wave.hpp -o pulse