Is there a recommended way to plot data using ascii symbols?

95 views Asked by At

I am writing a bare-metal C language test program that captures some sensor data, analyzes and plots it. The data is pretty simple and I only have a serial console interface. Still, I would like to plot the waveform using only mono-spaced ascii characters.

Is there a standard, recommended way to do something like this?

256  |           *
     |          *
     |            *
     |         *   *
128  |        *     *
     |       *       *       *  
     |   *                 ** *
     | ** ***         * * *    **
  0  _______________________________    
3

There are 3 answers

0
ikegami On BEST ANSWER

It's simply a pair of nested loops.

size_t n = 26;

for ( size_t y = 8; y--; ) {
   int miny = ( y + 0 ) * 8;
   int maxy = ( y + 1 ) * 8;

   for ( size_t x = 0; x < n; ++x ) {
      putchar( miny <= samples[ x ] && samples[ x ] < maxy ? '*' : ' ' );
   }

   putchar( '\n' );
}
1
Neil On

You could open a pipe to gnuplot, if available.

#include <stdlib.h>
#include <stdio.h>

int main(void) {
    FILE *gnu = popen("gnuplot", "w");
    if(!gnu) { perror("gnuplot"); return EXIT_FAILURE; }
    fprintf(gnu, "set term dumb\n"
        "$Data <<EOD\n"
        "10\n10\n70\n10\n10\n10\n"
        "100\n130\n160\n230\n260\n200\n"
        "160\n130\n100\n10\n10\n10\n60\n"
        "70\n80\n60\n20\n10\nEOD\n"
        "plot $Data\n");
    pclose(gnu);
    return EXIT_SUCCESS;
}

Gives me,

  300 +--------------------------------------------------------------------+   
      |             +             +            +             +             |   
      |                                                      $Data    A    |   
  250 |-+                         A                                      +-|   
      |                                                                    |   
      |                        A                                           |   
      |                                                                    |   
  200 |-+                           A                                    +-|   
      |                                                                    |   
      |                     A          A                                   |   
  150 |-+                                                                +-|   
      |                  A                A                                |   
      |                                                                    |   
  100 |-+              A                     A                           +-|   
      |                                                                    |   
      |     A                                             A  A             |   
      |                                                 A       A          |   
   50 |-+                                                                +-|   
      |                                                                    |   
      |  A    A  A  A             +            A  A  A       +     A A     |   
    0 +--------------------------------------------------------------------+   
      0             5             10           15            20            25  

You haven't really supplied the details of the capabilities of the terminal you are using. For example, you, or someone with access, may need to download the gnuplot package from your package manager.

0
Pedro_Uno On

Here is Ikegami's suggestion adapted to my environment.

void plot_samples(uint8_t *data, int n)
{
    const int n_rows = 32;
    const int max_val = 256;
    const int row_span = max_val/n_rows;

    xil_printf("%3d | ", max_val);
    for (int col=0; col<n; col++) xil_printf("-");
    xil_printf("|\n\r");
    int row_min_val;
    for (int row=n_rows-1; row>=0; row--){
        row_min_val = row*row_span;
        xil_printf("%3d | ", row_min_val);
        for (int col=0; col<n; col++){
            if (data[col] >= row_min_val) {
                xil_printf("*");
            } else {
                xil_printf(" ");
            }
        }
        xil_printf("|\n\r");
    }
    xil_printf("\n\r");
}

enter image description here