From 12f6bf916fbe4357305522cc361b522e2482e9f5 Mon Sep 17 00:00:00 2001 From: KIU Shueng Chuan Date: Tue, 17 Jan 2017 22:04:05 +0800 Subject: [PATCH 1/3] fix fft premature slicing away of 0 freq bin also fixes: - use rfft for better efficiency - use rfftfreq to compute coords correctly - works for both odd/even lengths - python3: integer division needed for numpy indexing --- pyqtgraph/graphicsItems/PlotDataItem.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pyqtgraph/graphicsItems/PlotDataItem.py b/pyqtgraph/graphicsItems/PlotDataItem.py index 37245bec..11184ae6 100644 --- a/pyqtgraph/graphicsItems/PlotDataItem.py +++ b/pyqtgraph/graphicsItems/PlotDataItem.py @@ -679,10 +679,9 @@ class PlotDataItem(GraphicsObject): x2 = np.linspace(x[0], x[-1], len(x)) y = np.interp(x2, x, y) x = x2 - f = np.fft.fft(y) / len(y) - y = abs(f[1:len(f)/2]) - dt = x[-1] - x[0] - x = np.linspace(0, 0.5*len(x)/dt, len(y)) + f = np.fft.rfft(y) / y.size + x = np.fft.rfftfreq(y.size) + y = np.abs(f) return x, y def dataType(obj): From 0a8d5b253aa70f9f03259604b53d51bf77791db8 Mon Sep 17 00:00:00 2001 From: KIU Shueng Chuan Date: Wed, 18 Jan 2017 09:02:53 +0800 Subject: [PATCH 2/3] fix: freq coords need to take into account x-coords spacing --- pyqtgraph/graphicsItems/PlotDataItem.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pyqtgraph/graphicsItems/PlotDataItem.py b/pyqtgraph/graphicsItems/PlotDataItem.py index 11184ae6..485576f6 100644 --- a/pyqtgraph/graphicsItems/PlotDataItem.py +++ b/pyqtgraph/graphicsItems/PlotDataItem.py @@ -679,8 +679,10 @@ class PlotDataItem(GraphicsObject): x2 = np.linspace(x[0], x[-1], len(x)) y = np.interp(x2, x, y) x = x2 - f = np.fft.rfft(y) / y.size - x = np.fft.rfftfreq(y.size) + n = y.size + f = np.fft.rfft(y) / n + d = (x[-1] - x[0]) / (n - 1) + x = np.fft.rfftfreq(n, d) y = np.abs(f) return x, y From 4553b55f736ba142619d9969bbd55bcda6db9824 Mon Sep 17 00:00:00 2001 From: KIU Shueng Chuan Date: Fri, 20 Jan 2017 09:09:18 +0800 Subject: [PATCH 3/3] python2 compat: don't assume true division --- pyqtgraph/graphicsItems/PlotDataItem.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyqtgraph/graphicsItems/PlotDataItem.py b/pyqtgraph/graphicsItems/PlotDataItem.py index 485576f6..a26c1c72 100644 --- a/pyqtgraph/graphicsItems/PlotDataItem.py +++ b/pyqtgraph/graphicsItems/PlotDataItem.py @@ -681,7 +681,7 @@ class PlotDataItem(GraphicsObject): x = x2 n = y.size f = np.fft.rfft(y) / n - d = (x[-1] - x[0]) / (n - 1) + d = float(x[-1]-x[0]) / (len(x)-1) x = np.fft.rfftfreq(n, d) y = np.abs(f) return x, y