Drawing graphs or plotting graphics in one dimension (x-y), we usually do by using a program such as excel Worksheet of the Spreadsheet from Microsoft Excel, Open Office, Gnumeric, or Matlab, and many more programs that can be used for this purpose. But when the data series is very long perhaps Excel Spreadsheet, Gnumeric, and OpenOfice were not able help us to draw a graph.
For it then we can only use other programs such as Matlab, GnuPlot, Octplot, Octave, Python, and so on.
Here, I am still learning this and try to share my experience of making or drawing a graph (x-y) signals from sound waves using Python.
The data series will be read data signals with the name "signals.inp" in ASCII such as format as follows,
—————————– file signals.txt —————————–
10000
0,-189
1.04166666666667e-05,-209
2.08333333333333e-05,-201
3.125e-05,-195
4.16666666666667e-05,-195
5.20833333333333e-05,-202
6.25e-05,-218
7.29166666666667e-05,-222
8.33333333333333e-05,-223
—————————– file signals.txt —————————–
The length of this file is 10,000 (ten thousand) line, in 2 (two) coma-separated fields.
—————————– file baca_signals.py —————————–
#!/usr/bin/env python
import matplotlib.pyplot as plt
from numpy import *
b = []
data = open(”signals.txt”,”r”)
m = data.readline()
baris = data.readlines()
for i in range(int(m)):
kolom = baris[i].split(”,”)
a = list(kolom)
b = b + a
c = list(b)
d = reshape(array(c),(float(m),2))
d1 = take(d,(0,),1)
d2 = take(d,(1,),1)
plt.plot(d1,d2)
plt.xlabel(’time (msec)’,fontsize=20)
plt.ylabel(’intensity’,fontsize=20)
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.axis([0,0.11,-6000,6000])
plt.title(’sound waves’,fontsize=25)
plt.grid(’true’)
plt.show()
—————————– file baca_signals.py —————————–
Python Script above is a script that used to read the file "signals .txt".
This Script can be run with the command as follows,
abuadnan@abuadnan-laptop:~/Documents$ ./baca_signals.py
Then, it will generate an image as follows,
Figure 1. The Plot of signal sound waves.
From Figure 1 above, the image signal can be stored. When the save icon in click then will appear the following image,
Figure 2. Save the image into a file *. png
After that it will appear with the image file name and format in accordance with the name and format choices we wish. See the file results in the following,
Figure 3. The Image File that is already stored and saved.
Figure 3 is a plot of signal sound waves that are read from the data file, drawn and saved in the image file format (in this case in "png" format).
So, hopefully this is helpful to all users of linux or windows that use Python as a free program (free softwares). This example File can be downloaded from here (50,8 Kb)
No comments:
Post a Comment