Quickly Plot in Python with Text File Values

Python has this wonderful command in numpy, np.loadtxt.
In this case, I use np.loadtxt to load a file consisting of several ADC real values to do a quick plot for visualization. The rule is that each row in the text file has to have the same number of elements. In this case it is simply.

2304
4233
3252
and so on...

 

import matplotlib.pyplot as plt
import numpy as np

data = np.loadtxt('/path/to/your/file')

# get x-axis bins
x_axis = np.arange(data.size) * 960 / data.size

#plot time domain
plt.plot(data)

# plot frequency domain
plt.figure()
spc = np.fft.fft(data)
plt.plot(x_axis, 20*np.log10(abs(spc)))

That’s all. Pretty quick and nifty!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: