Title: Incorrect and Inconsistent output of function PSD when scale_by_freq=False · Issue #4328 · matplotlib/matplotlib · GitHub
Open Graph Title: Incorrect and Inconsistent output of function PSD when scale_by_freq=False · Issue #4328 · matplotlib/matplotlib
X Title: Incorrect and Inconsistent output of function PSD when scale_by_freq=False · Issue #4328 · matplotlib/matplotlib
Description: If we state scale_by_freq=False, then we are asking for the power spectrum of the signal, instead of the power spectral density; i.e. units of V^2 instead of V^2/Hz, for example. mlab.psd, which actually is mlab._spectral_helper under th...
Open Graph Description: If we state scale_by_freq=False, then we are asking for the power spectrum of the signal, instead of the power spectral density; i.e. units of V^2 instead of V^2/Hz, for example. mlab.psd, which ac...
X Description: If we state scale_by_freq=False, then we are asking for the power spectrum of the signal, instead of the power spectral density; i.e. units of V^2 instead of V^2/Hz, for example. mlab.psd, which ac...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/4328
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Incorrect and Inconsistent output of function PSD when scale_by_freq=False","articleBody":"If we state `scale_by_freq=False`, then we are asking for the power spectrum of the signal, instead of the power spectral density; i.e. units of V^2 instead of V^2/Hz, for example. \n\n`mlab.psd`, which actually is `mlab._spectral_helper` under the hood, does not return the expected value of the power in a sinusoid. Furthermore, in contrast to the PSD case, it does not correct for the energy of the signal lost to the window function, meaning that the results differ depending on the window used. \n\nHere's an illustration. If we have a simple signal of a cosine oscillation happening at 100Hz with amplitude of 10, we would expect the power spectrum result at 100Hz to be 50, regardless of the window used. (A cosine like this that is identically zero at each integer second means we can use one second FFTs and compare the rectangular window to the hanning window, without worrying about any distortion)\n\n In MATLAB:\n\n```\nfs=2048;\ntt=[0:1/fs:60-1/fs];\nx = 10*cos(2*pi*100*tt);\n[pR, f]=pwelch(x, ones(1,fs), 0, fs, fs, 'power');\n[pH, f]=pwelch(x, hann(fs), 0, fs, fs, 'power');\n```\n\ngives `pR(101)==50` and `pH(101)==50.000...`, as expected. \n\nIn `matplotlib.mlab`:\n\n```\nfs = 2048\ntt = np.arange(fs*64)/float(fs)\nx = 10*np.cos(2*np.pi*100*tt)\n# One second FFT segments\npsdHann, ff = mlab.psd(x, Fs=fs, NFFT=fs, window=mlab.window_hanning, scale_by_freq=False)\npsdRect, ff = mlab.psd(x, Fs=fs, NFFT=fs, window=mlab.window_none, scale_by_freq=False)\n```\n\ngives `psdRect[100] == 102400`, which too large by a factor of `nfft` and `psdHann[100] == 68233.3`, which differs further by some window correction factor. \n\nI came across this in scipy/scipy#4682, where I was adapting the code for inclusion in scipy's signal processing repertoire. There, I used `scale = 1.0 / win.sum()**2` for the power spectrum case. I haven't looked for the exact places where `_spectral_helper` should be fixed, at this time. \n","author":{"url":"https://github.com/e-q","@type":"Person","name":"e-q"},"datePublished":"2015-04-12T02:18:33.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":3},"url":"https://github.com/4328/matplotlib/issues/4328"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:1849fd42-1cb8-8194-4612-beb11c2ad135 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9FF8:1A2FA1:A88859B:AC0EBF8:6A50BCFB |
| html-safe-nonce | 27701ca275cf4cab727a7a7b54599aa7b90c355e300e6face9324f7206c5ccc9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RkY4OjFBMkZBMTpBODg4NTlCOkFDMEVCRjg6NkE1MEJDRkIiLCJ2aXNpdG9yX2lkIjoiMTAzNzA4OTI3NDk1OTI4MTQwMyIsInJlZ2lvbl9lZGdlIjoic2VhIiwicmVnaW9uX3JlbmRlciI6InNlYSJ9 |
| visitor-hmac | 9dc1d49f520ae906af404df556573744f42595ad84dfe07c00a924c7c3f6807c |
| hovercard-subject-tag | issue:67862243 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/matplotlib/matplotlib/4328/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2468d1d3de7a750e4087e8a6492a09f2db23e42c00bffa6474f353f809eb2f7d/matplotlib/matplotlib/issues/4328 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2468d1d3de7a750e4087e8a6492a09f2db23e42c00bffa6474f353f809eb2f7d/matplotlib/matplotlib/issues/4328 |
| og:image:alt | If we state scale_by_freq=False, then we are asking for the power spectrum of the signal, instead of the power spectral density; i.e. units of V^2 instead of V^2/Hz, for example. mlab.psd, which ac... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | e-q |
| hostname | github.com |
| expected-hostname | github.com |
| None | 72b13e0e8d0319acdd27a145cfe73f4f06c791713d0ac4690e41fb68ad28cac0 |
| turbo-cache-control | no-preview |
| go-import | github.com/matplotlib/matplotlib git https://github.com/matplotlib/matplotlib.git |
| octolytics-dimension-user_id | 215947 |
| octolytics-dimension-user_login | matplotlib |
| octolytics-dimension-repository_id | 1385122 |
| octolytics-dimension-repository_nwo | matplotlib/matplotlib |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1385122 |
| octolytics-dimension-repository_network_root_nwo | matplotlib/matplotlib |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 08e79d53fc070870d5b168356afebb1e286a2ffe |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width