{"id":1131903,"date":"2025-01-08T20:52:28","date_gmt":"2025-01-08T12:52:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1131903.html"},"modified":"2025-01-08T20:52:30","modified_gmt":"2025-01-08T12:52:30","slug":"python%e5%a6%82%e4%bd%95%e7%94%bb%e5%87%ba%e4%b8%80%e7%bb%84%e6%95%b0%e6%8d%ae%e7%9a%84%e9%a2%91%e8%b0%b1%e5%9b%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1131903.html","title":{"rendered":"python\u5982\u4f55\u753b\u51fa\u4e00\u7ec4\u6570\u636e\u7684\u9891\u8c31\u56fe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101537\/00e436be-c34f-40c1-8865-2003fc43ded5.webp\" alt=\"python\u5982\u4f55\u753b\u51fa\u4e00\u7ec4\u6570\u636e\u7684\u9891\u8c31\u56fe\" \/><\/p>\n<p><p> <strong>Python\u5982\u4f55\u753b\u51fa\u4e00\u7ec4\u6570\u636e\u7684\u9891\u8c31\u56fe<\/strong><br \/><strong>\u4f7f\u7528Python\u7ed8\u5236\u4e00\u7ec4\u6570\u636e\u7684\u9891\u8c31\u56fe\uff0c\u53ef\u4ee5\u4f7f\u7528\u5e93\u5982NumPy\u3001SciPy\u548cMatplotlib\uff0c\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u53ef\u89c6\u5316\u529f\u80fd\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u6570\u636e\u9884\u5904\u7406\u3001\u8ba1\u7b97\u5085\u91cc\u53f6\u53d8\u6362\u3001\u7ed8\u5236\u9891\u8c31\u56fe\u3002<\/strong>\u5176\u4e2d\uff0c\u8ba1\u7b97\u5085\u91cc\u53f6\u53d8\u6362\u662f\u5173\u952e\u6b65\u9aa4\uff0c\u5085\u91cc\u53f6\u53d8\u6362\u5c06\u65f6\u95f4\u57df\u7684\u6570\u636e\u8f6c\u6362\u4e3a\u9891\u57df\uff0c\u63ed\u793a\u6570\u636e\u4e2d\u7684\u9891\u7387\u6210\u5206\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u6570\u636e\u9884\u5904\u7406<\/p>\n<p>\u5728\u7ed8\u5236\u9891\u8c31\u56fe\u4e4b\u524d\uff0c\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\u3002\u9884\u5904\u7406\u6b65\u9aa4\u5305\u62ec\u53bb\u9664\u566a\u58f0\u3001\u5f52\u4e00\u5316\u4ee5\u53ca\u62c6\u5206\u6570\u636e\u7b49\u3002\u6570\u636e\u9884\u5904\u7406\u7684\u76ee\u7684\u662f\u63d0\u9ad8\u9891\u8c31\u56fe\u7684\u51c6\u786e\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6570\u636e\u53bb\u566a<\/strong><\/p>\n<p>\u53bb\u566a\u5904\u7406\u53ef\u4ee5\u53bb\u9664\u6570\u636e\u4e2d\u7684\u9ad8\u9891\u566a\u58f0\u6210\u5206\uff0c\u4f7f\u9891\u8c31\u56fe\u66f4\u52a0\u6e05\u6670\u3002\u53bb\u566a\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5982\u79fb\u52a8\u5e73\u5747\u6cd5\u3001\u4f4e\u901a\u6ee4\u6ce2\u5668\u7b49\u3002Python\u4e2d\u53ef\u4ee5\u4f7f\u7528SciPy\u5e93\u4e2d\u7684\u4fe1\u53f7\u5904\u7406\u6a21\u5757\u6765\u5b9e\u73b0\u53bb\u566a\u5904\u7406\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u5f52\u4e00\u5316<\/strong><\/p>\n<p>\u6570\u636e\u5f52\u4e00\u5316\u53ef\u4ee5\u5c06\u6570\u636e\u7f29\u653e\u5230\u4e00\u4e2a\u7279\u5b9a\u7684\u8303\u56f4\uff0c\u901a\u5e38\u662f0\u52301\u4e4b\u95f4\uff0c\u8fd9\u6837\u53ef\u4ee5\u6d88\u9664\u6570\u503c\u5927\u5c0f\u5bf9\u9891\u8c31\u56fe\u7684\u5f71\u54cd\u3002\u5f52\u4e00\u5316\u5904\u7406\u5e38\u7528\u7684\u65b9\u6cd5\u6709\u6700\u5c0f-\u6700\u5927\u5f52\u4e00\u5316\u3001Z-score\u6807\u51c6\u5316\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u62c6\u5206<\/strong><\/p>\n<p>\u5bf9\u4e8e\u957f\u65f6\u95f4\u5e8f\u5217\u6570\u636e\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u62c6\u5206\u6210\u591a\u4e2a\u5c0f\u6bb5\uff0c\u5206\u522b\u8ba1\u7b97\u548c\u7ed8\u5236\u9891\u8c31\u56fe\u3002\u8fd9\u6837\u53ef\u4ee5\u66f4\u597d\u5730\u89c2\u5bdf\u6570\u636e\u5728\u4e0d\u540c\u65f6\u95f4\u6bb5\u7684\u9891\u7387\u53d8\u5316\u60c5\u51b5\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import scipy.signal as signal<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>fs = 1000  # \u91c7\u6837\u9891\u7387<\/p>\n<p>t = np.arange(0, 1, 1\/fs)  # \u65f6\u95f4\u5e8f\u5217<\/p>\n<p>x = np.sin(2*np.pi*50*t) + np.sin(2*np.pi*120*t)  # \u751f\u6210\u5305\u542b\u4e24\u4e2a\u9891\u7387\u6210\u5206\u7684\u4fe1\u53f7<\/p>\n<h2><strong>\u6570\u636e\u53bb\u566a\uff08\u79fb\u52a8\u5e73\u5747\u6cd5\uff09<\/strong><\/h2>\n<p>window_size = 10<\/p>\n<p>x_smooth = np.convolve(x, np.ones(window_size)\/window_size, mode=&#39;same&#39;)<\/p>\n<h2><strong>\u6570\u636e\u5f52\u4e00\u5316<\/strong><\/h2>\n<p>x_normalized = (x_smooth - np.min(x_smooth)) \/ (np.max(x_smooth) - np.min(x_smooth))<\/p>\n<h2><strong>\u6570\u636e\u62c6\u5206<\/strong><\/h2>\n<p>segments = np.split(x_normalized, 10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u8ba1\u7b97\u5085\u91cc\u53f6\u53d8\u6362<\/p>\n<p>\u5085\u91cc\u53f6\u53d8\u6362\u662f\u5c06\u65f6\u95f4\u57df\u7684\u4fe1\u53f7\u8f6c\u6362\u4e3a\u9891\u57df\u4fe1\u53f7\u7684\u6570\u5b66\u53d8\u6362\u3002\u901a\u8fc7\u5085\u91cc\u53f6\u53d8\u6362\uff0c\u53ef\u4ee5\u83b7\u5f97\u4fe1\u53f7\u7684\u9891\u8c31\uff0c\u5373\u5404\u4e2a\u9891\u7387\u6210\u5206\u7684\u5e45\u5ea6\u548c\u76f8\u4f4d\u4fe1\u606f\u3002Python\u4e2d\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u7684<code>fft<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u5085\u91cc\u53f6\u53d8\u6362\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5085\u91cc\u53f6\u53d8\u6362<\/strong><\/p>\n<p>\u5085\u91cc\u53f6\u53d8\u6362\u5c06\u65f6\u95f4\u5e8f\u5217\u4fe1\u53f7\u8f6c\u6362\u4e3a\u9891\u57df\u4fe1\u53f7\uff0c\u5f97\u5230\u5404\u4e2a\u9891\u7387\u6210\u5206\u7684\u5e45\u5ea6\u548c\u76f8\u4f4d\u4fe1\u606f\u3002\u5085\u91cc\u53f6\u53d8\u6362\u7684\u7ed3\u679c\u901a\u5e38\u662f\u590d\u6570\uff0c\u9700\u8981\u8ba1\u7b97\u5176\u7edd\u5bf9\u503c\u6765\u5f97\u5230\u5e45\u5ea6\u8c31\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9891\u7387\u8f74\u751f\u6210<\/strong><\/p>\n<p>\u9891\u7387\u8f74\u662f\u9891\u8c31\u56fe\u7684\u6a2a\u8f74\uff0c\u8868\u793a\u5404\u4e2a\u9891\u7387\u6210\u5206\u7684\u9891\u7387\u503c\u3002\u9891\u7387\u8f74\u7684\u751f\u6210\u53ef\u4ee5\u901a\u8fc7NumPy\u5e93\u7684<code>fft.fftfreq<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5e45\u5ea6\u8c31\u8ba1\u7b97<\/strong><\/p>\n<p>\u5e45\u5ea6\u8c31\u8868\u793a\u5404\u4e2a\u9891\u7387\u6210\u5206\u7684\u5e45\u5ea6\u5927\u5c0f\uff0c\u53ef\u4ee5\u901a\u8fc7\u8ba1\u7b97\u5085\u91cc\u53f6\u53d8\u6362\u7ed3\u679c\u7684\u7edd\u5bf9\u503c\u6765\u5f97\u5230\u3002\u901a\u5e38\uff0c\u5e45\u5ea6\u8c31\u9700\u8981\u5f52\u4e00\u5316\u5904\u7406\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u663e\u793a\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u5085\u91cc\u53f6\u53d8\u6362<\/p>\n<p>fft_result = np.fft.fft(x_normalized)<\/p>\n<p>fft_magnitude = np.abs(fft_result)<\/p>\n<h2><strong>\u751f\u6210\u9891\u7387\u8f74<\/strong><\/h2>\n<p>frequencies = np.fft.fftfreq(len(x_normalized), 1\/fs)<\/p>\n<h2><strong>\u7ed8\u5236\u5e45\u5ea6\u8c31<\/strong><\/h2>\n<p>plt.figure(figsize=(10, 6))<\/p>\n<p>plt.plot(frequencies, fft_magnitude)<\/p>\n<p>plt.title(&#39;Frequency Spectrum&#39;)<\/p>\n<p>plt.xlabel(&#39;Frequency (Hz)&#39;)<\/p>\n<p>plt.ylabel(&#39;Magnitude&#39;)<\/p>\n<p>plt.grid(True)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u7ed8\u5236\u9891\u8c31\u56fe<\/p>\n<p>\u7ed8\u5236\u9891\u8c31\u56fe\u662f\u5c06\u8ba1\u7b97\u5f97\u5230\u7684\u9891\u7387\u6210\u5206\u548c\u5e45\u5ea6\u4fe1\u606f\u8fdb\u884c\u53ef\u89c6\u5316\u5c55\u793a\u3002Python\u4e2d\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u6765\u7ed8\u5236\u9891\u8c31\u56fe\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8bbe\u7f6e\u56fe\u5f62\u53c2\u6570<\/strong><\/p>\n<p>\u8bbe\u7f6e\u56fe\u5f62\u7684\u5927\u5c0f\u3001\u6807\u9898\u3001\u5750\u6807\u8f74\u6807\u7b7e\u7b49\u53c2\u6570\uff0c\u4f7f\u56fe\u5f62\u66f4\u52a0\u7f8e\u89c2\u548c\u6613\u8bfb\u3002\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u4e2d\u7684<code>figure<\/code>\u3001<code>title<\/code>\u3001<code>xlabel<\/code>\u3001<code>ylabel<\/code>\u7b49\u51fd\u6570\u6765\u8bbe\u7f6e\u56fe\u5f62\u53c2\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7ed8\u5236\u9891\u8c31\u56fe<\/strong><\/p>\n<p>\u4f7f\u7528Matplotlib\u5e93\u4e2d\u7684<code>plot<\/code>\u51fd\u6570\u6765\u7ed8\u5236\u9891\u8c31\u56fe\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u9891\u8c31\u56fe\u7684\u6a2a\u8f74\u662f\u9891\u7387\uff0c\u7eb5\u8f74\u662f\u5e45\u5ea6\u3002\u53ef\u4ee5\u4f7f\u7528<code>grid<\/code>\u51fd\u6570\u6765\u6dfb\u52a0\u7f51\u683c\u7ebf\uff0c\u4f7f\u56fe\u5f62\u66f4\u6613\u8bfb\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/p>\n<p>\u4f7f\u7528Matplotlib\u5e93\u4e2d\u7684<code>show<\/code>\u51fd\u6570\u6765\u663e\u793a\u56fe\u5f62\u3002\u53ef\u4ee5\u5c06\u56fe\u5f62\u4fdd\u5b58\u4e3a\u56fe\u7247\u6587\u4ef6\uff0c\u65b9\u4fbf\u540e\u7eed\u4f7f\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6e\u56fe\u5f62\u53c2\u6570<\/p>\n<p>plt.figure(figsize=(10, 6))<\/p>\n<p>plt.title(&#39;Frequency Spectrum&#39;)<\/p>\n<p>plt.xlabel(&#39;Frequency (Hz)&#39;)<\/p>\n<p>plt.ylabel(&#39;Magnitude&#39;)<\/p>\n<h2><strong>\u7ed8\u5236\u9891\u8c31\u56fe<\/strong><\/h2>\n<p>plt.plot(frequencies, fft_magnitude)<\/p>\n<p>plt.grid(True)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f18\u5316\u9891\u8c31\u56fe<\/p>\n<p>\u9891\u8c31\u56fe\u7684\u4f18\u5316\u53ef\u4ee5\u63d0\u9ad8\u56fe\u5f62\u7684\u53ef\u8bfb\u6027\u548c\u51c6\u786e\u6027\u3002\u4f18\u5316\u65b9\u6cd5\u5305\u62ec\u9891\u7387\u5206\u8fa8\u7387\u8c03\u6574\u3001\u5e45\u5ea6\u8c31\u5e73\u6ed1\u5904\u7406\u3001\u9891\u8c31\u56fe\u989c\u8272\u6620\u5c04\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9891\u7387\u5206\u8fa8\u7387\u8c03\u6574<\/strong><\/p>\n<p>\u9891\u7387\u5206\u8fa8\u7387\u662f\u6307\u9891\u8c31\u56fe\u4e2d\u76f8\u90bb\u9891\u7387\u70b9\u4e4b\u95f4\u7684\u95f4\u9694\u3002\u9891\u7387\u5206\u8fa8\u7387\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u91c7\u6837\u9891\u7387\u548c\u91c7\u6837\u70b9\u6570\u6765\u6539\u53d8\u3002\u8f83\u9ad8\u7684\u9891\u7387\u5206\u8fa8\u7387\u53ef\u4ee5\u66f4\u51c6\u786e\u5730\u53cd\u6620\u4fe1\u53f7\u7684\u9891\u7387\u6210\u5206\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5e45\u5ea6\u8c31\u5e73\u6ed1\u5904\u7406<\/strong><\/p>\n<p>\u5e45\u5ea6\u8c31\u5e73\u6ed1\u5904\u7406\u53ef\u4ee5\u51cf\u5c11\u9891\u8c31\u56fe\u4e2d\u7684\u9ad8\u9891\u566a\u58f0\uff0c\u4f7f\u56fe\u5f62\u66f4\u52a0\u6e05\u6670\u3002\u5e73\u6ed1\u5904\u7406\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5982\u79fb\u52a8\u5e73\u5747\u6cd5\u3001\u9ad8\u65af\u5e73\u6ed1\u7b49\u3002Python\u4e2d\u53ef\u4ee5\u4f7f\u7528SciPy\u5e93\u4e2d\u7684\u4fe1\u53f7\u5904\u7406\u6a21\u5757\u6765\u5b9e\u73b0\u5e73\u6ed1\u5904\u7406\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9891\u8c31\u56fe\u989c\u8272\u6620\u5c04<\/strong><\/p>\n<p>\u9891\u8c31\u56fe\u989c\u8272\u6620\u5c04\u53ef\u4ee5\u901a\u8fc7\u4e0d\u540c\u989c\u8272\u8868\u793a\u4e0d\u540c\u7684\u5e45\u5ea6\u503c\uff0c\u4f7f\u56fe\u5f62\u66f4\u52a0\u76f4\u89c2\u3002\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u4e2d\u7684<code>imshow<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u989c\u8272\u6620\u5c04\u3002\u989c\u8272\u6620\u5c04\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5982\u70ed\u56fe\u3001\u7070\u5ea6\u56fe\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u9891\u7387\u5206\u8fa8\u7387\u8c03\u6574<\/p>\n<p>new_fs = 2000  # \u65b0\u7684\u91c7\u6837\u9891\u7387<\/p>\n<p>new_t = np.arange(0, 1, 1\/new_fs)<\/p>\n<p>new_x = np.sin(2*np.pi*50*new_t) + np.sin(2*np.pi*120*new_t)<\/p>\n<p>new_fft_result = np.fft.fft(new_x)<\/p>\n<p>new_fft_magnitude = np.abs(new_fft_result)<\/p>\n<p>new_frequencies = np.fft.fftfreq(len(new_x), 1\/new_fs)<\/p>\n<h2><strong>\u5e45\u5ea6\u8c31\u5e73\u6ed1\u5904\u7406\uff08\u9ad8\u65af\u5e73\u6ed1\uff09<\/strong><\/h2>\n<p>smoothed_magnitude = signal.gaussian(len(new_fft_magnitude), std=7)<\/p>\n<p>smoothed_fft_magnitude = new_fft_magnitude * smoothed_magnitude<\/p>\n<h2><strong>\u9891\u8c31\u56fe\u989c\u8272\u6620\u5c04<\/strong><\/h2>\n<p>plt.figure(figsize=(10, 6))<\/p>\n<p>plt.imshow(smoothed_fft_magnitude.reshape(1, -1), aspect=&#39;auto&#39;, cmap=&#39;hot&#39;, extent=[new_frequencies.min(), new_frequencies.max(), 0, 1])<\/p>\n<p>plt.colorbar(label=&#39;Magnitude&#39;)<\/p>\n<p>plt.title(&#39;Smoothed Frequency Spectrum with Color Mapping&#39;)<\/p>\n<p>plt.xlabel(&#39;Frequency (Hz)&#39;)<\/p>\n<p>plt.ylabel(&#39;Magnitude&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5e94\u7528\u5b9e\u4f8b<\/p>\n<p>\u7ed8\u5236\u9891\u8c31\u56fe\u7684\u5e94\u7528\u975e\u5e38\u5e7f\u6cdb\uff0c\u5305\u62ec\u4fe1\u53f7\u5904\u7406\u3001\u97f3\u9891\u5206\u6790\u3001\u632f\u52a8\u5206\u6790\u7b49\u9886\u57df\u3002\u4e0b\u9762\u4ecb\u7ecd\u51e0\u4e2a\u5e38\u89c1\u7684\u5e94\u7528\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u97f3\u9891\u4fe1\u53f7\u5206\u6790<\/strong><\/p>\n<p>\u97f3\u9891\u4fe1\u53f7\u5206\u6790\u662f\u9891\u8c31\u56fe\u7684\u4e00\u4e2a\u91cd\u8981\u5e94\u7528\u3002\u901a\u8fc7\u9891\u8c31\u56fe\uff0c\u53ef\u4ee5\u5206\u6790\u97f3\u9891\u4fe1\u53f7\u7684\u9891\u7387\u6210\u5206\uff0c\u68c0\u6d4b\u97f3\u9891\u4e2d\u7684\u566a\u58f0\u548c\u5931\u771f\u7b49\u95ee\u9898\u3002Python\u4e2d\u53ef\u4ee5\u4f7f\u7528Librosa\u5e93\u6765\u5904\u7406\u97f3\u9891\u4fe1\u53f7\uff0c\u5e76\u7ed8\u5236\u9891\u8c31\u56fe\u3002<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">import librosa<\/p>\n<p>import librosa.display<\/p>\n<h2><strong>\u8bfb\u53d6\u97f3\u9891\u4fe1\u53f7<\/strong><\/h2>\n<p>audio_path = &#39;example.wav&#39;<\/p>\n<p>y, sr = librosa.load(audio_path, sr=None)<\/p>\n<h2><strong>\u8ba1\u7b97\u5085\u91cc\u53f6\u53d8\u6362<\/strong><\/h2>\n<p>audio_fft_result = np.fft.fft(y)<\/p>\n<p>audio_fft_magnitude = np.abs(audio_fft_result)<\/p>\n<p>audio_frequencies = np.fft.fftfreq(len(y), 1\/sr)<\/p>\n<h2><strong>\u7ed8\u5236\u9891\u8c31\u56fe<\/strong><\/h2>\n<p>plt.figure(figsize=(10, 6))<\/p>\n<p>plt.plot(audio_frequencies, audio_fft_magnitude)<\/p>\n<p>plt.title(&#39;Audio Frequency Spectrum&#39;)<\/p>\n<p>plt.xlabel(&#39;Frequency (Hz)&#39;)<\/p>\n<p>plt.ylabel(&#39;Magnitude&#39;)<\/p>\n<p>plt.grid(True)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u632f\u52a8\u4fe1\u53f7\u5206\u6790<\/strong><\/p>\n<p>\u632f\u52a8\u4fe1\u53f7\u5206\u6790\u662f\u9891\u8c31\u56fe\u7684\u53e6\u4e00\u4e2a\u91cd\u8981\u5e94\u7528\u3002\u901a\u8fc7\u9891\u8c31\u56fe\uff0c\u53ef\u4ee5\u5206\u6790\u632f\u52a8\u4fe1\u53f7\u7684\u9891\u7387\u6210\u5206\uff0c\u68c0\u6d4b\u673a\u68b0\u8bbe\u5907\u7684\u6545\u969c\u548c\u5f02\u5e38\u7b49\u95ee\u9898\u3002Python\u4e2d\u53ef\u4ee5\u4f7f\u7528SciPy\u5e93\u6765\u5904\u7406\u632f\u52a8\u4fe1\u53f7\uff0c\u5e76\u7ed8\u5236\u9891\u8c31\u56fe\u3002<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u632f\u52a8\u4fe1\u53f7<\/p>\n<p>vibration_fs = 1000  # \u91c7\u6837\u9891\u7387<\/p>\n<p>vibration_t = np.arange(0, 1, 1\/vibration_fs)<\/p>\n<p>vibration_x = np.sin(2*np.pi*30*vibration_t) + np.sin(2*np.pi*80*vibration_t)<\/p>\n<h2><strong>\u8ba1\u7b97\u5085\u91cc\u53f6\u53d8\u6362<\/strong><\/h2>\n<p>vibration_fft_result = np.fft.fft(vibration_x)<\/p>\n<p>vibration_fft_magnitude = np.abs(vibration_fft_result)<\/p>\n<p>vibration_frequencies = np.fft.fftfreq(len(vibration_x), 1\/vibration_fs)<\/p>\n<h2><strong>\u7ed8\u5236\u9891\u8c31\u56fe<\/strong><\/h2>\n<p>plt.figure(figsize=(10, 6))<\/p>\n<p>plt.plot(vibration_frequencies, vibration_fft_magnitude)<\/p>\n<p>plt.title(&#39;Vibration Frequency Spectrum&#39;)<\/p>\n<p>plt.xlabel(&#39;Frequency (Hz)&#39;)<\/p>\n<p>plt.ylabel(&#39;Magnitude&#39;)<\/p>\n<p>plt.grid(True)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u7ed8\u5236\u4e00\u7ec4\u6570\u636e\u7684\u9891\u8c31\u56fe\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u6570\u636e\u9884\u5904\u7406\u3001\u5085\u91cc\u53f6\u53d8\u6362\u3001\u9891\u8c31\u56fe\u7ed8\u5236\u3001\u9891\u8c31\u56fe\u4f18\u5316\u548c\u5e94\u7528\u5b9e\u4f8b\u7b49\u5185\u5bb9\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u9891\u8c31\u56fe\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\u6765\u7ed8\u5236\u9891\u8c31\u56fe\uff0c\u6700\u5e38\u7528\u7684\u662fMatplotlib\u548cNumPy\u3002\u9996\u5148\uff0c\u9700\u8981\u51c6\u5907\u597d\u6570\u636e\uff0c\u7136\u540e\u901a\u8fc7\u5085\u91cc\u53f6\u53d8\u6362\u5c06\u6570\u636e\u8f6c\u6362\u5230\u9891\u57df\uff0c\u6700\u540e\u4f7f\u7528Matplotlib\u7ed8\u5236\u9891\u8c31\u56fe\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u3001\u751f\u6210\u6216\u52a0\u8f7d\u6570\u636e\u3001\u6267\u884c\u5085\u91cc\u53f6\u53d8\u6362\u4ee5\u53ca\u8bbe\u7f6e\u7ed8\u56fe\u53c2\u6570\u3002<\/p>\n<p><strong>\u9891\u8c31\u56fe\u7684\u5e94\u7528\u573a\u666f\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u9891\u8c31\u56fe\u5728\u4fe1\u53f7\u5904\u7406\u3001\u97f3\u9891\u5206\u6790\u3001\u901a\u4fe1\u7cfb\u7edf\u7b49\u591a\u4e2a\u9886\u57df\u90fd\u6709\u5e7f\u6cdb\u5e94\u7528\u3002\u5b83\u53ef\u4ee5\u7528\u6765\u5206\u6790\u97f3\u9891\u4fe1\u53f7\u7684\u9891\u7387\u6210\u5206\uff0c\u8bc6\u522b\u4fe1\u53f7\u4e2d\u7684\u5468\u671f\u6027\u53d8\u5316\uff0c\u5e2e\u52a9\u5728\u566a\u58f0\u8fc7\u6ee4\u3001\u6570\u636e\u538b\u7f29\u548c\u7279\u5f81\u63d0\u53d6\u7b49\u4efb\u52a1\u4e2d\u505a\u51fa\u51b3\u7b56\u3002<\/p>\n<p><strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u53c2\u6570\u4ee5\u83b7\u53d6\u6e05\u6670\u7684\u9891\u8c31\u56fe\uff1f<\/strong><br \/>\u5728\u7ed8\u5236\u9891\u8c31\u56fe\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u91c7\u6837\u9891\u7387\u548c\u7a97\u53e3\u51fd\u6570\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u91c7\u6837\u9891\u7387\u5e94\u81f3\u5c11\u4e3a\u4fe1\u53f7\u4e2d\u6700\u9ad8\u9891\u7387\u7684\u4e24\u500d\uff0c\u4ee5\u907f\u514d\u6df7\u53e0\u73b0\u8c61\u3002\u7a97\u53e3\u51fd\u6570\u7684\u9009\u62e9\u5f71\u54cd\u9891\u8c31\u56fe\u7684\u5206\u8fa8\u7387\u548c\u6cc4\u6f0f\u73b0\u8c61\uff0c\u5e38\u7528\u7684\u7a97\u53e3\u51fd\u6570\u5305\u62ec\u6c49\u5b81\u7a97\u548c\u6c49\u660e\u7a97\u3002\u6839\u636e\u5177\u4f53\u6570\u636e\u7684\u7279\u6027\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4e0d\u540c\u7684\u53c2\u6570\u8bbe\u7f6e\uff0c\u4ee5\u83b7\u5f97\u6700\u4f73\u7684\u9891\u8c31\u56fe\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5982\u4f55\u753b\u51fa\u4e00\u7ec4\u6570\u636e\u7684\u9891\u8c31\u56fe\u4f7f\u7528Python\u7ed8\u5236\u4e00\u7ec4\u6570\u636e\u7684\u9891\u8c31\u56fe\uff0c\u53ef\u4ee5\u4f7f\u7528\u5e93\u5982NumPy\u3001SciPy\u548c [&hellip;]","protected":false},"author":3,"featured_media":1131911,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1131903"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1131903"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1131903\/revisions"}],"predecessor-version":[{"id":1131912,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1131903\/revisions\/1131912"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1131911"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1131903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1131903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1131903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}