printplot.cpp (1638B)
1 #include <iostream> 2 #include <string> 3 #include <math.h> 4 #define PI2 6.28318530718 5 6 using namespace std; 7 void pp(double* signal, int N, int M) 8 { 9 // N is signal length. M is sample resolution. 10 double threshold[M-1]; 11 double sampled[N]; 12 // If M is odd: 13 if(M % 2){ 14 15 } 16 // If M is even: 17 else { 18 for(int l = 0; l < M-1; l++) 19 threshold[l] = double(2*l - (M - 2)) / M; 20 } 21 22 // height is the resolution (in characters of each sample) 23 // width is the number of samples 24 for(int n = 0; n < N; n++) { 25 for(int l = 0; l < M-1; l++){ 26 if(signal[n] < threshold[l] ){ 27 sampled[n] = l; 28 break; 29 } 30 if(l == M-2){ 31 sampled[n] = M; 32 break; 33 } 34 } 35 } 36 // Plot: 37 for(int m = M; m > 0; m--) { 38 // Evaluated each character 39 for(int n = 0; n < N; n++) 40 { 41 if(sampled[n] == m && n > 1){ 42 if(sampled[n] < sampled[n-1] && sampled[n-1] < sampled[n-2]) 43 cout << "\\"; 44 else if(sampled[n] > sampled[n-1] && sampled[n-1] > sampled[n-2]) 45 cout << "/"; 46 else 47 cout << "-"; 48 } 49 else 50 cout << " "; 51 } 52 cout << endl; 53 } 54 } 55 56 int main() 57 { 58 double freq; 59 int N = 100; 60 double signal[N]; 61 62 // Create a sin to use as example 63 cout << "Frequency: "; 64 cin >> freq; 65 for(int n = 0; n < N; n++) { 66 signal[n] = sin(PI2*freq*n/N); 67 } 68 pp(signal, N, 30); 69 70 return 0; 71 } 72