experiments

All kinds of coding experiments
Log | Files | Refs | Submodules

printplot2.cpp (1526B)


      1 #include <iostream>
      2 #include <string>
      3 #include <math.h>
      4 #define PI2 6.28318530718
      5 
      6 // THIS IS CURRENTLY NOT WORKING
      7 using namespace std;
      8 void pp(double* signal, int N, int res)
      9 {
     10     // N is signal length. res is plot resolution.
     11     int M = 65536;  // Bit depth of 16 bits.
     12     double threshold[M-1];
     13     double sampled[N];
     14     for(int l = 0; l < M-1; l++)
     15         threshold[l] = double(2*l - (M - 2)) / M;
     16 
     17     // "Sample" the input signal to 16 bit int
     18     for(int n = 0; n < N; n++) {
     19         for(int l = 0; l < M-1; l++){
     20             if(signal[n] < threshold[l] ){
     21                 sampled[n] = l;
     22                 break;
     23             }
     24             if(l == M-2){
     25                 sampled[n] = M;
     26                 break;
     27             }
     28         }
     29     }
     30     // Plot:
     31     for(int line = res; line > 0; line--) {
     32         // Evaluated each character
     33         for(int n = 0; n < N; n++)
     34         {
     35             if(sampled[n]/M == line && n > 0){
     36                 if(sampled[n] < sampled[n-1])
     37                     cout << "\\";
     38                 else if(sampled[n] > sampled[n-1])
     39                     cout << "/";
     40                 else
     41                     cout << "-";
     42             }
     43             else
     44                 cout << " ";
     45         }
     46         cout << endl;
     47     }
     48 }
     49 
     50 int main()
     51 {
     52     double freq;
     53     int N = 100;
     54     double signal[N];
     55 
     56     // Create a sin to use as example
     57     cout << "Frequency: ";
     58     cin >> freq;
     59     for(int n = 0; n < N; n++) {
     60         signal[n] = sin(PI2*freq*n/N); 
     61     }
     62     pp(signal, N, 30);
     63 
     64     return 0;
     65 }
     66