bernsee.com > The DSP Dimension > Fun Stuff > Ascii Art

ASCII Art - Plotting a Waveform On The Console

I often run into the problem of showing people, either personally or on remotely through email or usenet groups, a certain output of a DSP process. But: some people work on UNIX, others on Windows or on the Mac. Writing code that draws on the screen for each platform is tedious, and unnerving if you only want to display some basic things like waveforms or spectra at a reasonably coarse resolution. Also, when posting to usenet news groups, binary attachments are either not permitted or not possible, and are stripped off the message when it is stored in an archive. Therefore, most people "draw" their waveforms by typing appropriate characters for each point, which we call "ASCII Art".

smbPlotWave() does exactly that. It draws a discrete signal, stored in a float buffer, via ASCII Art on the screen. It does this at a fairly coarse resolution, but it does it nicely and is entirely cross platform. Make sure you use a monospace font, though! The resulting "image" can be copied and pasted into an email, which is very convenient. It is also great for debugging purposes, when you don't have the time to bother with graphics.

smbPlotWave() only uses stdio commands, which are available on all platforms.
Enjoy!

 

Listing 1.0: smbPlotWave displays a discrete waveform via ASCII art

#define DISP_STR_MAXLEN     256 
                                    
bool smbPlotWave(float *data, long rowLength)
//Prints waveform row-wise to the console
//(c) 1999 S.M.Bernsee, made available under the
WOL
//Returns false when something wet wrong, true otherwise
//data[] contains the data to be displayed. It is assumed that the
//data values lie between -1.0 and +1.0, please normalize your
//data accordingly
//rowLength is the length of all rows. Usually, this is between 60-80
//characters
{ char printString[DISP_STR_MAXLEN]; long numRows = 21; if (data == 0) return false; if (rowLength > DISP_STR_MAXLEN-3) return false; for (long i = 0; i < numRows; i++) { sprintf(printString, "%3.0d >", -i+numRows/2); long lastQdata = 0; for (long j = 0; j < rowLength; j++) { long qData = (long)((float)(numRows-1)*(.5+.5*data[j])+.5); if (qData == i) { if (qData-lastQdata > 0) sprintf(printString, "%s`", printString); else if (qData-lastQdata < 0) sprintf(printString, "%s,", printString); else sprintf(printString, "%s-", printString); } else { if (i == numRows/2) sprintf(printString, "%s-", printString); else sprintf(printString, "%s ", printString); } lastQdata = qData; } sprintf(printString, "%s\n", printString); printf("%s",printString); } printf("\n"); return true; }


Last change: 04.10.2006, ©2002-2006 S. M. Bernsee, all rights reserved. Content subject to change without notice. Content provided 'as is', see disclaimer.