Some example code from Bengt Richter to generate and graph a sine wave in ascii.

   1 #--< sinewave. py >-------------------------------------------
   2 # Try this ( just run  python sinewave.py and it should generate 2 seconds
   3 # of 2khz at 75% amplitude that you can listen to with media player.
   4 # Also will plot the first 34 samples on console screen, self-scaling
   5 # when it exceeds the display region (right away here). See after this.
   6 # My media player didn't like 4-byte samples, so that was changed, among
   7 # other things ;-)
   8 import math
   9 import wave
  10 import array
  11 
  12 pi = math.pi
  13 twopi = 2.0*pi
  14 sin = math.sin
  15 cos = math.cos
  16 
  17 #init output and related vars
  18 samplesPerSecond = 44100
  19 Outfile = wave.open("outfile.wav", "w")
  20 Outfile.setnchannels(1)
  21 Outfile.setsampwidth(2) # 2 bytes for 32767 amplitude
  22 Outfile.setframerate(samplesPerSecond)
  23 Outfile.setcomptype("NONE", "Uncompressed")
  24 
  25 def gen_wav(seconds, frequency, amplitudePercentOfMax):
  26     # calculate frequency as cycles/sample
  27     cyclesPerSample = float(frequency)/samplesPerSecond
  28     numberOfSamples = int(seconds*samplesPerSecond)
  29     maxAmplitude = (amplitudePercentOfMax/100.)*32767
  30     sampleArray = array.array('h')
  31 
  32     for nSamples in xrange(numberOfSamples):
  33         cycles = nSamples*cyclesPerSample
  34         cycles -= int(cycles) # makes 0.0<=cycles<1.0
  35         sampleValue = int(round(maxAmplitude*sin(twopi*cycles))) # round 
  36         sampleArray.append(sampleValue)
  37     return sampleArray
  38 
  39 if __name__ == '__main__':
  40 
  41     #Outtemp = gen_wav(.01, 440, 75)
  42     # generate .001 sec = 44.1 samples & 2 cycles with 20 amplitude
  43     #sampleArray = gen_wav(.00075, 2000, (20./32767.)*100)
  44     sampleArray = gen_wav(2, 2000, 75)
  45 
  46     # print first 34 samples as selfscaling ascii plot
  47     grid = list(' |%19s|%19s|' %('',''))
  48     scale = 1.0; maximumMagnitude = 20
  49     for i in range(min(34,len(sampleArray))):
  50         sample= sampleArray[i]
  51         if abs(sample)>maximumMagnitude:
  52             maximumMagnitude=abs(sample)
  53             scale = 20./maximumMagnitude
  54         x= int(round(sampleArray[i]*scale))+21
  55         print ''.join(grid[:x]+['*']+grid[x+1:])
  56 
  57     Outfile.writeframes(sampleArray.tostring())
  58     Outfile.close()
  59 #-------------------------------------------------------------

Output from above:

[14:27] C:\pywk\clp>python sinewave.py
 |                   *                   |
 |                   |                   *
 |                   |                   *
 |                   |                   *
 |                   |                   *
 |                   |                   *
 |                   |                   *
 |                   |                 * |
 |                   |              *    |
 |                   |          *        |
 |                   |     *             |
 |                   *                   |
 |             *     |                   |
 |        *          |                   |
 |    *              |                   |
 | *                 |                   |
 *                   |                   |
 *                   |                   |
 | *                 |                   |
 |    *              |                   |
 |        *          |                   |
 |             *     |                   |
 |                   *                   |
 |                   |    *              |
 |                   |          *        |
 |                   |              *    |
 |                   |                 * |
 |                   |                   *
 |                   |                   *
 |                   |                 * |
 |                   |              *    |
 |                   |          *        |
 |                   |     *             |
 |                   *                   |

Projects/Python/DSP/GenerateAndASCIIGraphSineWave (last edited 2008-11-16 00:31:48 by NathanRamella)