3
votes

I use this code part;

df = pd.read_csv('Vertikale_Netzlast_2013.csv', header=6, sep=';', parse_dates=[[0, 1]], index_col=0, na_values=['n.v.'])
df.columns = ["time", "Load"]
df.Load = df.Load.interpolate()
plt.figure(figsize=(14, 5))
df.Load.plot()
plt.title('Vertical Grid Load Germany 2013')
plt.ylabel('Power [$MW$]')
plt.savefig('VerticalGridLoadGermany2013.png', bbox_inches='tight', dpi=150, transparent=True)
hann = np.hanning(len(df.Load.values))
Y = np.fft.fft(hann * df.Load.values)
N = ceil(len(Y) / 2 + 1)
print(N)
fa = 1.0 / (15.0 * 60.0) # every 15 minutes
print('fa=%.4fHz (Frequency)' % fa)
X = np.linspace(0, fa / 2, N, endpoint=True)
plt.plot(X, 2.0 * np.abs(Y[:N]) / N)
plt.xlabel('Frequency ($Hz$)')
plt.ylabel('vertical powergrid load ($MW$)')

But I don't run and python gives this error

/home/ozlemb/PycharmProjects/work/venv/lib/python3.6/site-packages/numpy/core/numeric.py:531: ComplexWarning: Casting complex values to real discards the imaginary part return array(a, dtype, copy=False, order=order) dt=0.00629s (Sample Time) fa=159.00Hz (Frequency) /home/ozlemb/PycharmProjects/work/venv/lib/python3.6/site-packages/dateutil/parser/_parser.py:1204: UnknownTimezoneWarning: tzname B identified but not understood. Pass tzinfos argument in order to correctly return a timezone-aware datetime. In a future version, this will raise an exception. category=UnknownTimezoneWarning) 17523 fa=0.0011Hz (Frequency) Exception in Tkinter callback Traceback (most recent call last): File "/usr/lib/python3.6/tkinter/init.py", line 1702, in call__return self.func(*args) File "/usr/lib/python3.6/tkinter/__init.py", line 746, in callit func(*args) File "/home/ozlemb/PycharmProjects/work/venv/lib/python3.6/site-packages/matplotlib/backends/_backend_tk.py", line 346, in idle_draw self.draw() File "/home/ozlemb/PycharmProjects/work/venv/lib/python3.6/site-packages/matplotlib/backends/backend_tkagg.py", line 9, in draw super(FigureCanvasTkAgg, self).draw() File "/home/ozlemb/PycharmProjects/work/venv/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py", line 402, in draw self.figure.draw(self.renderer) File "/home/ozlemb/PycharmProjects/work/venv/lib/python3.6/site-packages/matplotlib/artist.py", line 50, in draw_wrapper return draw(artist, renderer, *args, **kwargs) File "/home/ozlemb/PycharmProjects/work/venv/lib/python3.6/site-packages/matplotlib/figure.py", line 1652, in draw renderer, self, artists, self.suppressComposite) File "/home/ozlemb/PycharmProjects/work/venv/lib/python3.6/site-packages/matplotlib/image.py", line 138, in _draw_list_compositing_images a.draw(renderer) File "/home/ozlemb/PycharmProjects/work/venv/lib/python3.6/site-packages/matplotlib/artist.py", line 50, in draw_wrapper return draw(artist, renderer, *args, **kwargs) File "/home/ozlemb/PycharmProjects/work/venv/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 2604, in draw mimage._draw_list_compositing_images(renderer, self, artists) File "/home/ozlemb/PycharmProjects/work/venv/lib/python3.6/site-packages/matplotlib/image.py", line 138, in _draw_list_compositing_images a.draw(renderer) File "/home/ozlemb/PycharmProjects/work/venv/lib/python3.6/site-packages/matplotlib/artist.py", line 50, in draw_wrapper return draw(artist, renderer, *args, **kwargs) File "/home/ozlemb/PycharmProjects/work/venv/lib/python3.6/site-packages/matplotlib/axis.py", line 1185, in draw ticks_to_draw = self._update_ticks(renderer) File "/home/ozlemb/PycharmProjects/work/venv/lib/python3.6/site-packages/matplotlib/axis.py", line 1023, in _update_ticks tick_tups = list(self.iter_ticks()) # iter_ticks calls the locator File "/home/ozlemb/PycharmProjects/work/venv/lib/python3.6/site-packages/matplotlib/axis.py", line 967, in iter_ticks majorLocs = self.major.locator() File "/home/ozlemb/PycharmProjects/work/venv/lib/python3.6/site-packages/matplotlib/dates.py", line 1229, in call self.refresh() File "/home/ozlemb/PycharmProjects/work/venv/lib/python3.6/site-packages/matplotlib/dates.py", line 1249, in refresh dmin, dmax = self.viewlim_to_dt() File "/home/ozlemb/PycharmProjects/work/venv/lib/python3.6/site-packages/matplotlib/dates.py", line 1000, in viewlim_to_dt.format(vmin)) ValueError: view limit minimum -36761.69947916667 is less than 1 and is an invalid Matplotlib date value. This often happens if you pass a non-datetime value to an axis that has datetime units

Process finished with exit code 0

Does this cause the python (3) version?? I am new and I want to processing signal data from sensors with fft.

Thank you

2
Please include the full error message. It should say which line is the problem.timgeb

2 Answers

1
votes

If you are using Python3 N = len(Y)/2+1 can evaluate to a non-integer value. After that you will be trying to slice a list with Y[:N] but N could be equal to 2.5 for example.

See the following code:

nums = [1, 2, 3, 4, 5, 6, 7]
N = len(nums) / 2 + 1
print("N =", N);
print(nums[:N])

The way you are calculating N will make it equal to 4.5 because len(nums) / 2 is 3.5. I would instead do from math import ceil and then

N = ceil(len(nums) / 2);

This will make N equal to 4.

This is of course depends on what exactly you are trying to slice. The main point here is that you have to make sure that N is an integer and not a float value.

0
votes

Does this line gives error?

X = np.linspace(0, fa/2, N, endpoint=True)

If so it must be caused from the fact that N must be integer. In your case N might be float

Let's check umpy reference;

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)

num : int, optional

Number of samples to generate.

Default is 50. Must be non-negative.

[https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.linspace.html]