summaryrefslogtreecommitdiff
path: root/gr-utils/python/utils/plot_fft_base.py
diff options
context:
space:
mode:
Diffstat (limited to 'gr-utils/python/utils/plot_fft_base.py')
-rw-r--r--[-rwxr-xr-x]gr-utils/python/utils/plot_fft_base.py68
1 files changed, 34 insertions, 34 deletions
diff --git a/gr-utils/python/utils/plot_fft_base.py b/gr-utils/python/utils/plot_fft_base.py
index c4bc484d97..5040aefa87 100755..100644
--- a/gr-utils/python/utils/plot_fft_base.py
+++ b/gr-utils/python/utils/plot_fft_base.py
@@ -20,22 +20,26 @@
# Boston, MA 02110-1301, USA.
#
+from __future__ import print_function
+from __future__ import division
+from __future__ import unicode_literals
+
try:
import scipy
from scipy import fftpack
except ImportError:
- print "Please install SciPy to run this script (http://www.scipy.org/)"
- raise SystemExit, 1
+ print("Please install SciPy to run this script (http://www.scipy.org/)")
+ raise SystemExit(1)
try:
from pylab import *
except ImportError:
- print "Please install Python Matplotlib (http://matplotlib.sourceforge.net/) and Python TkInter https://wiki.python.org/moin/TkInter to run this script"
- raise SystemExit, 1
+ print("Please install Python Matplotlib (http://matplotlib.sourceforge.net/) and Python TkInter https://wiki.python.org/moin/TkInter to run this script")
+ raise SystemExit(1)
-from optparse import OptionParser
+from argparse import ArgumentParser
-class plot_fft_base:
+class plot_fft_base(object):
def __init__(self, datatype, filename, options):
self.hfile = open(filename, "r")
self.block_length = options.block
@@ -79,33 +83,33 @@ class plot_fft_base:
show()
def get_data(self):
- self.position = self.hfile.tell()/self.sizeof_data
+ self.position = self.hfile.tell() / self.sizeof_data
self.text_file_pos.set_text("File Position: %d" % (self.position))
try:
self.iq = scipy.fromfile(self.hfile, dtype=self.datatype, count=self.block_length)
except MemoryError:
- print "End of File"
+ print("End of File")
else:
self.iq_fft = self.dofft(self.iq)
tstep = 1.0 / self.sample_rate
- #self.time = scipy.array([tstep*(self.position + i) for i in xrange(len(self.iq))])
- self.time = scipy.array([tstep*(i) for i in xrange(len(self.iq))])
+ #self.time = scipy.array([tstep*(self.position + i) for i in range(len(self.iq))])
+ self.time = scipy.array([tstep*(i) for i in range(len(self.iq))])
self.freq = self.calc_freq(self.time, self.sample_rate)
def dofft(self, iq):
N = len(iq)
iq_fft = scipy.fftpack.fftshift(scipy.fft(iq)) # fft and shift axis
- iq_fft = 20*scipy.log10(abs((iq_fft+1e-15)/N)) # convert to decibels, adjust power
+ iq_fft = 20*scipy.log10(abs((iq_fft+1e-15) / N)) # convert to decibels, adjust power
# adding 1e-15 (-300 dB) to protect against value errors if an item in iq_fft is 0
return iq_fft
def calc_freq(self, time, sample_rate):
N = len(time)
- Fs = 1.0 / (time.max() - time.min())
+ Fs = 1.0 / (time.max( - time.min()))
Fn = 0.5 * sample_rate
- freq = scipy.array([-Fn + i*Fs for i in xrange(N)])
+ freq = scipy.array([-Fn + i*Fs for i in range(N)])
return freq
def make_plots(self):
@@ -209,41 +213,37 @@ class plot_fft_base:
@staticmethod
def setup_options():
- usage="%prog: [options] input_filename"
description = "Takes a GNU Radio complex binary file and displays the I&Q data versus time as well as the frequency domain (FFT) plot. The y-axis values are plotted assuming volts as the amplitude of the I&Q streams and converted into dBm in the frequency domain (the 1/N power adjustment out of the FFT is performed internally). The script plots a certain block of data at a time, specified on the command line as -B or --block. This value defaults to 1000. The start position in the file can be set by specifying -s or --start and defaults to 0 (the start of the file). By default, the system assumes a sample rate of 1, so in time, each sample is plotted versus the sample number. To set a true time and frequency axis, set the sample rate (-R or --sample-rate) to the sample rate used when capturing the samples."
- parser = OptionParser(conflict_handler="resolve", usage=usage, description=description)
- parser.add_option("-d", "--data-type", type="string", default="complex64",
- help="Specify the data type (complex64, float32, (u)int32, (u)int16, (u)int8) [default=%default]")
- parser.add_option("-B", "--block", type="int", default=1000,
- help="Specify the block size [default=%default]")
- parser.add_option("-s", "--start", type="int", default=0,
- help="Specify where to start in the file [default=%default]")
- parser.add_option("-R", "--sample-rate", type="float", default=1.0,
- help="Set the sampler rate of the data [default=%default]")
+ parser = ArgumentParser(conflict_handler="resolve", description=description)
+ parser.add_argument("-d", "--data-type", default="complex64",
+ choices=("complex64", "float32", "uint32", "int32", "uint16",
+ "int16", "uint8", "int8"),
+ help="Specify the data type [default=%(default)r]")
+ parser.add_argument("-B", "--block", type=int, default=1000,
+ help="Specify the block size [default=%(default)r]")
+ parser.add_argument("-s", "--start", type=int, default=0,
+ help="Specify where to start in the file [default=%(default)r]")
+ parser.add_argument("-R", "--sample-rate", type=float, default=1.0,
+ help="Set the sampler rate of the data [default=%(default)r]")
+ parser.add_argument("file", metavar="FILE",
+ help="Input file with samples")
return parser
def find(item_in, list_search):
try:
- return list_search.index(item_in) != None
+ return list_search.index(item_in) != None
except ValueError:
- return False
+ return False
def main():
parser = plot_fft_base.setup_options()
- (options, args) = parser.parse_args ()
- if len(args) != 1:
- parser.print_help()
- raise SystemExit, 1
- filename = args[0]
+ args = parser.parse_args()
- dc = plot_fft_base(options.data_type, filename, options)
+ dc = plot_fft_base(args.data_type, args.file, args)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
pass
-
-
-