Forum Archive

Why do i get diffrent graphs running this code with Python 2.7 or 3.5?

KlMa

Hi,
I wanted to see if i could find out the centrifugation speed of my old washing machine using some vibration analysis in Numpy using fft etc. So i wrote a little script that captured accelerometer data at 80Hz for one minute and then placed my ipad on top of the machine during centrifugation. I stored the data in an array in an .npy file. My graphs looked ok, I think, with a dominant peak at about 12Hz(12*60=720rpm...this probably explains why my clothes are so wet coming out from the machine). I tried the same using the the psd function in matplotlib using Python 2.7 and got a similar result, but when i run the code in Python 3.5 I get a very diffrent result. Can somebody explain why? Is it the diffrent versions Python or matplotlib or Pythonista or something else, i dont get it? The .npy file can be found here

# coding: utf-8
import console
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as ml

# console.clear()
file_name = 'wc-cent-2.npy'
data = np.load(file_name)

fig_mag = plt.figure()
plt.psd(data[:, 0], NFFT=256, Fs=80, window=ml.window_hanning, detrend = ml.detrend_none, scale_by_freq = True, noverlap = 0, pad_to = None, sides = 'onesided')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Power/Frequency (dB/Hz)')
plt.title('PSD X')
plt.show()
JonB

There are different versions of matplotlib and numpy.
That is really bizarre though! The 2.7 plot looks more like what the fft of the data is. The 3.5 is a little too convienent happens to be a spike right at Fs/4???, and, for instance, the detrend does not seem to work (spike at 0). down sampling the data also shows that the 3.5 is garbage data[0:-1:2,0]

Both seem to work ok with a simple sinusoid.

It might be worth poking around on matplotlib to see the differences between 1.3.1 (py 2.7) and 1.4, or betwen 1.4 and later versions (maybe a problem in psd was fixed).

I also wonder if @omz ever ran the matplotlib test suite? I had some other problems with the 3.5 version of matplotlib (crash when using matplotlib.use('template'))

(by the way, as a matplotlib user, you might be interested in https://github.com/jsbain/backend_pythonista)

KlMa

Apparently there is a bug in matplotlib 1.4.0 related to sliced arrays. A work around is to flattening the input.

# coding: utf-8
import console
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as ml

# console.clear()
file_name = 'wc-cent-2.npy'
data = np.load(file_name)

fig_mag = plt.figure()
plt.psd(data[:, 0].flatten(), NFFT=256, Fs=80, window=ml.window_hanning, detrend = ml.detrend_none, scale_by_freq = True, noverlap = 0, pad_to = None, sides = 'onesided')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Power/Frequency (dB/Hz)')
plt.title('PSD X')
plt.show()
RHH

Would you post the script that captures the accelerometer data? Sounds like a clever use of the iPad.

KlMa

Here you can find the capture script, and here a script that processes the array and creates a few plots. Any help with Y axis units are appreciated. Make sure you secure the ipad before testing :)