{"id":1173998,"date":"2025-01-15T17:10:57","date_gmt":"2025-01-15T09:10:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1173998.html"},"modified":"2025-01-15T17:10:59","modified_gmt":"2025-01-15T09:10:59","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e6%b1%82%e5%b1%80%e9%83%a8%e9%ab%98%e7%82%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1173998.html","title":{"rendered":"\u5982\u4f55\u7528python\u6c42\u5c40\u90e8\u9ad8\u70b9"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26075628\/ace9f897-1c2a-40f2-831b-9f43fd289309.webp\" alt=\"\u5982\u4f55\u7528python\u6c42\u5c40\u90e8\u9ad8\u70b9\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u7528Python\u6c42\u5c40\u90e8\u9ad8\u70b9<\/strong><\/p>\n<\/p>\n<p><p><strong>\u5229\u7528SciPy\u5e93\u3001\u4f7f\u7528numpy\u6570\u7ec4\u3001\u7ed3\u5408\u4fe1\u53f7\u5904\u7406\u65b9\u6cd5<\/strong>\uff0c\u8fd9\u4e9b\u662f\u7528Python\u6c42\u5c40\u90e8\u9ad8\u70b9\u7684\u4e3b\u8981\u65b9\u6cd5\u3002<strong>\u5229\u7528SciPy\u5e93<\/strong>\u662f\u5176\u4e2d\u4e00\u4e2a\u975e\u5e38\u6709\u6548\u7684\u65b9\u5f0f\uff0c\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e00\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u5229\u7528SciPy\u5e93<\/strong><\/p>\n<\/p>\n<p><p>SciPy\u5e93\u662f\u4e00\u4e2a\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684Python\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u6570\u5b66\u3001\u79d1\u5b66\u548c\u5de5\u7a0b\u7684\u51fd\u6570\u3002\u901a\u8fc7\u4f7f\u7528SciPy\u5e93\u4e2d\u7684<code>find_peaks<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u627e\u5230\u6570\u636e\u4e2d\u7684\u5c40\u90e8\u9ad8\u70b9\u3002<code>find_peaks<\/code>\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u6570\u7ec4\uff0c\u5e76\u8fd4\u56de\u8be5\u6570\u7ec4\u4e2d\u5c40\u90e8\u9ad8\u70b9\u7684\u7d22\u5f15\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from scipy.signal import find_peaks<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6570\u7ec4<\/strong><\/h2>\n<p>data = np.array([0, 2, 1, 3, 1, 2, 0, 5, 1, 0])<\/p>\n<h2><strong>\u4f7f\u7528find_peaks\u51fd\u6570\u627e\u5230\u5c40\u90e8\u9ad8\u70b9<\/strong><\/h2>\n<p>peaks, _ = find_peaks(data)<\/p>\n<p>print(&quot;\u5c40\u90e8\u9ad8\u70b9\u7684\u7d22\u5f15:&quot;, peaks)<\/p>\n<p>print(&quot;\u5c40\u90e8\u9ad8\u70b9\u7684\u503c:&quot;, data[peaks])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>find_peaks<\/code>\u51fd\u6570\u627e\u5230\u5c40\u90e8\u9ad8\u70b9\u7684\u7d22\u5f15\u4e3a[1, 3, 7]\uff0c\u5bf9\u5e94\u7684\u6570\u636e\u503c\u5206\u522b\u4e3a2, 3, 5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5229\u7528SciPy\u5e93<\/h3>\n<\/p>\n<p><p>SciPy\u5e93\u4e2d\u7684<code>find_peaks<\/code>\u51fd\u6570\u662f\u5bfb\u627e\u6570\u636e\u4e2d\u5c40\u90e8\u9ad8\u70b9\u7684\u6709\u6548\u5de5\u5177\u3002\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u627e\u5230\u9ad8\u70b9\u7684\u7d22\u5f15\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u53c2\u6570\u6765\u6ee1\u8db3\u5404\u79cd\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>find_peaks<\/code>\u51fd\u6570\u7684\u57fa\u672c\u7528\u6cd5\u5982\u524d\u6240\u8ff0\u3002\u5b83\u63a5\u53d7\u4e00\u4e2a\u6570\u7ec4\uff0c\u5e76\u8fd4\u56de\u8be5\u6570\u7ec4\u4e2d\u5c40\u90e8\u9ad8\u70b9\u7684\u7d22\u5f15\u3002\u8be5\u51fd\u6570\u8fd8\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u66f4\u591a\u4fe1\u606f\u7684\u5b57\u5178\uff0c\u5982\u5cf0\u503c\u7684\u9ad8\u5ea6\u3001\u5bbd\u5ea6\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from scipy.signal import find_peaks<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6570\u7ec4<\/strong><\/h2>\n<p>data = np.array([0, 2, 1, 3, 1, 2, 0, 5, 1, 0])<\/p>\n<h2><strong>\u4f7f\u7528find_peaks\u51fd\u6570\u627e\u5230\u5c40\u90e8\u9ad8\u70b9<\/strong><\/h2>\n<p>peaks, properties = find_peaks(data)<\/p>\n<p>print(&quot;\u5c40\u90e8\u9ad8\u70b9\u7684\u7d22\u5f15:&quot;, peaks)<\/p>\n<p>print(&quot;\u5c40\u90e8\u9ad8\u70b9\u7684\u503c:&quot;, data[peaks])<\/p>\n<p>print(&quot;\u5cf0\u503c\u7684\u5c5e\u6027:&quot;, properties)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8c03\u6574\u53c2\u6570<\/h4>\n<\/p>\n<p><p><code>find_peaks<\/code>\u51fd\u6570\u63d0\u4f9b\u4e86\u8bb8\u591a\u53ef\u9009\u53c2\u6570\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u7cbe\u786e\u5730\u627e\u5230\u9700\u8981\u7684\u5c40\u90e8\u9ad8\u70b9\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7<code>height<\/code>\u53c2\u6570\u6765\u8bbe\u7f6e\u5cf0\u503c\u7684\u6700\u5c0f\u9ad8\u5ea6\uff0c\u901a\u8fc7<code>distance<\/code>\u53c2\u6570\u6765\u8bbe\u7f6e\u76f8\u90bb\u5cf0\u503c\u4e4b\u95f4\u7684\u6700\u5c0f\u8ddd\u79bb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from scipy.signal import find_peaks<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6570\u7ec4<\/strong><\/h2>\n<p>data = np.array([0, 2, 1, 3, 1, 2, 0, 5, 1, 0])<\/p>\n<h2><strong>\u8bbe\u7f6e\u5cf0\u503c\u7684\u6700\u5c0f\u9ad8\u5ea6\u4e3a2<\/strong><\/h2>\n<p>peaks, _ = find_peaks(data, height=2)<\/p>\n<p>print(&quot;\u9ad8\u5ea6\u5927\u4e8e\u7b49\u4e8e2\u7684\u5c40\u90e8\u9ad8\u70b9\u7684\u7d22\u5f15:&quot;, peaks)<\/p>\n<p>print(&quot;\u9ad8\u5ea6\u5927\u4e8e\u7b49\u4e8e2\u7684\u5c40\u90e8\u9ad8\u70b9\u7684\u503c:&quot;, data[peaks])<\/p>\n<h2><strong>\u8bbe\u7f6e\u76f8\u90bb\u5cf0\u503c\u4e4b\u95f4\u7684\u6700\u5c0f\u8ddd\u79bb\u4e3a2<\/strong><\/h2>\n<p>peaks, _ = find_peaks(data, distance=2)<\/p>\n<p>print(&quot;\u76f8\u90bb\u5cf0\u503c\u4e4b\u95f4\u6700\u5c0f\u8ddd\u79bb\u4e3a2\u7684\u5c40\u90e8\u9ad8\u70b9\u7684\u7d22\u5f15:&quot;, peaks)<\/p>\n<p>print(&quot;\u76f8\u90bb\u5cf0\u503c\u4e4b\u95f4\u6700\u5c0f\u8ddd\u79bb\u4e3a2\u7684\u5c40\u90e8\u9ad8\u70b9\u7684\u503c:&quot;, data[peaks])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u8fd4\u56de\u66f4\u591a\u5c5e\u6027<\/h4>\n<\/p>\n<p><p><code>find_peaks<\/code>\u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u8fd4\u56de\u5c40\u90e8\u9ad8\u70b9\u7684\u7d22\u5f15\uff0c\u8fd8\u53ef\u4ee5\u8fd4\u56de\u66f4\u591a\u7684\u5c5e\u6027\u4fe1\u606f\uff0c\u5982\u5cf0\u503c\u7684\u9ad8\u5ea6\u3001\u5bbd\u5ea6\u3001\u5761\u5ea6\u7b49\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e9b\u5c5e\u6027\u6765\u8fdb\u4e00\u6b65\u5206\u6790\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from scipy.signal import find_peaks<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6570\u7ec4<\/strong><\/h2>\n<p>data = np.array([0, 2, 1, 3, 1, 2, 0, 5, 1, 0])<\/p>\n<h2><strong>\u4f7f\u7528find_peaks\u51fd\u6570\u627e\u5230\u5c40\u90e8\u9ad8\u70b9\uff0c\u5e76\u8fd4\u56de\u66f4\u591a\u5c5e\u6027<\/strong><\/h2>\n<p>peaks, properties = find_peaks(data, height=1, width=1)<\/p>\n<p>print(&quot;\u5c40\u90e8\u9ad8\u70b9\u7684\u7d22\u5f15:&quot;, peaks)<\/p>\n<p>print(&quot;\u5c40\u90e8\u9ad8\u70b9\u7684\u503c:&quot;, data[peaks])<\/p>\n<p>print(&quot;\u5cf0\u503c\u7684\u9ad8\u5ea6:&quot;, properties[&#39;peak_heights&#39;])<\/p>\n<p>print(&quot;\u5cf0\u503c\u7684\u5bbd\u5ea6:&quot;, properties[&#39;widths&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528numpy\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u5229\u7528SciPy\u5e93\uff0c<strong>\u4f7f\u7528numpy\u6570\u7ec4<\/strong>\u4e5f\u662f\u4e00\u79cd\u6709\u6548\u7684\u65b9\u6cd5\u3002\u901a\u8fc7numpy\u6570\u7ec4\u7684\u7d22\u5f15\u548c\u5207\u7247\u529f\u80fd\uff0c\u53ef\u4ee5\u624b\u52a8\u627e\u5230\u5c40\u90e8\u9ad8\u70b9\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u6bd4\u8f83\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u627e\u5230\u5c40\u90e8\u9ad8\u70b9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6570\u7ec4<\/strong><\/h2>\n<p>data = np.array([0, 2, 1, 3, 1, 2, 0, 5, 1, 0])<\/p>\n<h2><strong>\u627e\u5230\u5c40\u90e8\u9ad8\u70b9<\/strong><\/h2>\n<p>peaks = (np.diff(np.sign(np.diff(data))) &lt; 0).nonzero()[0] + 1<\/p>\n<p>print(&quot;\u5c40\u90e8\u9ad8\u70b9\u7684\u7d22\u5f15:&quot;, peaks)<\/p>\n<p>print(&quot;\u5c40\u90e8\u9ad8\u70b9\u7684\u503c:&quot;, data[peaks])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8c03\u6574\u6761\u4ef6<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u8c03\u6574\u6761\u4ef6\uff0c\u53ef\u4ee5\u627e\u5230\u6ee1\u8db3\u7279\u5b9a\u8981\u6c42\u7684\u5c40\u90e8\u9ad8\u70b9\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u5c40\u90e8\u9ad8\u70b9\u7684\u6700\u5c0f\u9ad8\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6570\u7ec4<\/strong><\/h2>\n<p>data = np.array([0, 2, 1, 3, 1, 2, 0, 5, 1, 0])<\/p>\n<h2><strong>\u627e\u5230\u9ad8\u5ea6\u5927\u4e8e\u7b49\u4e8e2\u7684\u5c40\u90e8\u9ad8\u70b9<\/strong><\/h2>\n<p>peaks = (np.diff(np.sign(np.diff(data))) &lt; 0).nonzero()[0] + 1<\/p>\n<p>peaks = peaks[data[peaks] &gt;= 2]<\/p>\n<p>print(&quot;\u9ad8\u5ea6\u5927\u4e8e\u7b49\u4e8e2\u7684\u5c40\u90e8\u9ad8\u70b9\u7684\u7d22\u5f15:&quot;, peaks)<\/p>\n<p>print(&quot;\u9ad8\u5ea6\u5927\u4e8e\u7b49\u4e8e2\u7684\u5c40\u90e8\u9ad8\u70b9\u7684\u503c:&quot;, data[peaks])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7ed3\u5408\u4fe1\u53f7\u5904\u7406\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u5229\u7528SciPy\u5e93\u548cnumpy\u6570\u7ec4\uff0c<strong>\u7ed3\u5408\u4fe1\u53f7\u5904\u7406\u65b9\u6cd5<\/strong>\u4e5f\u662f\u4e00\u79cd\u6709\u6548\u7684\u65b9\u5f0f\u3002\u901a\u8fc7\u5e94\u7528\u4fe1\u53f7\u5904\u7406\u4e2d\u7684\u5e73\u6ed1\u548c\u6ee4\u6ce2\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u627e\u5230\u5c40\u90e8\u9ad8\u70b9\u7684\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><h4>1. \u5e73\u6ed1\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u5e94\u7528\u5e73\u6ed1\u65b9\u6cd5\uff0c\u53ef\u4ee5\u51cf\u5c11\u6570\u636e\u4e2d\u7684\u566a\u58f0\uff0c\u4ece\u800c\u66f4\u51c6\u786e\u5730\u627e\u5230\u5c40\u90e8\u9ad8\u70b9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u4f7f\u7528\u5377\u79ef\u6765\u5e73\u6ed1\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from scipy.signal import find_peaks<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6570\u7ec4<\/strong><\/h2>\n<p>data = np.array([0, 2, 1, 3, 1, 2, 0, 5, 1, 0])<\/p>\n<h2><strong>\u4f7f\u7528\u5377\u79ef\u5e73\u6ed1\u6570\u636e<\/strong><\/h2>\n<p>window = np.ones(3) \/ 3<\/p>\n<p>smoothed_data = np.convolve(data, window, mode=&#39;same&#39;)<\/p>\n<h2><strong>\u4f7f\u7528find_peaks\u51fd\u6570\u627e\u5230\u5c40\u90e8\u9ad8\u70b9<\/strong><\/h2>\n<p>peaks, _ = find_peaks(smoothed_data)<\/p>\n<p>print(&quot;\u5e73\u6ed1\u540e\u5c40\u90e8\u9ad8\u70b9\u7684\u7d22\u5f15:&quot;, peaks)<\/p>\n<p>print(&quot;\u5e73\u6ed1\u540e\u5c40\u90e8\u9ad8\u70b9\u7684\u503c:&quot;, data[peaks])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5e94\u7528\u6ee4\u6ce2\u5668<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u5e94\u7528\u6ee4\u6ce2\u5668\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u51cf\u5c11\u6570\u636e\u4e2d\u7684\u566a\u58f0\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4f4e\u901a\u6ee4\u6ce2\u5668\u6765\u5e73\u6ed1\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from scipy.signal import find_peaks, butter, filtfilt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6570\u7ec4<\/strong><\/h2>\n<p>data = np.array([0, 2, 1, 3, 1, 2, 0, 5, 1, 0])<\/p>\n<h2><strong>\u521b\u5efa\u4f4e\u901a\u6ee4\u6ce2\u5668<\/strong><\/h2>\n<p>b, a = butter(3, 0.1)<\/p>\n<h2><strong>\u5e94\u7528\u6ee4\u6ce2\u5668\u5e73\u6ed1\u6570\u636e<\/strong><\/h2>\n<p>filtered_data = filtfilt(b, a, data)<\/p>\n<h2><strong>\u4f7f\u7528find_peaks\u51fd\u6570\u627e\u5230\u5c40\u90e8\u9ad8\u70b9<\/strong><\/h2>\n<p>peaks, _ = find_peaks(filtered_data)<\/p>\n<p>print(&quot;\u6ee4\u6ce2\u540e\u5c40\u90e8\u9ad8\u70b9\u7684\u7d22\u5f15:&quot;, peaks)<\/p>\n<p>print(&quot;\u6ee4\u6ce2\u540e\u5c40\u90e8\u9ad8\u70b9\u7684\u503c:&quot;, data[peaks])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5b9e\u9645\u5e94\u7528\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u627e\u5230\u6570\u636e\u4e2d\u7684\u5c40\u90e8\u9ad8\u70b9\u53ef\u4ee5\u7528\u4e8e\u8bb8\u591a\u573a\u666f\uff0c\u5982\u4fe1\u53f7\u5904\u7406\u3001\u56fe\u50cf\u5904\u7406\u3001\u91d1\u878d\u6570\u636e\u5206\u6790\u7b49\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5b9e\u9645\u5e94\u7528\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>1. \u4fe1\u53f7\u5904\u7406\u4e2d\u7684\u5c40\u90e8\u9ad8\u70b9<\/h4>\n<\/p>\n<p><p>\u5728\u4fe1\u53f7\u5904\u7406\u9886\u57df\uff0c\u627e\u5230\u4fe1\u53f7\u4e2d\u7684\u5c40\u90e8\u9ad8\u70b9\u53ef\u4ee5\u7528\u4e8e\u68c0\u6d4b\u4e8b\u4ef6\u3001\u7279\u5f81\u63d0\u53d6\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u4f7f\u7528SciPy\u5e93\u627e\u5230\u4fe1\u53f7\u4e2d\u7684\u5c40\u90e8\u9ad8\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from scipy.signal import find_peaks<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u4fe1\u53f7\u6570\u636e<\/strong><\/h2>\n<p>time = np.linspace(0, 10, 100)<\/p>\n<p>signal = np.sin(time) + 0.5 * np.random.normal(size=len(time))<\/p>\n<h2><strong>\u4f7f\u7528find_peaks\u51fd\u6570\u627e\u5230\u5c40\u90e8\u9ad8\u70b9<\/strong><\/h2>\n<p>peaks, _ = find_peaks(signal)<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u7ed8\u5236\u4fe1\u53f7\u548c\u5c40\u90e8\u9ad8\u70b9<\/strong><\/h2>\n<p>plt.plot(time, signal, label=&#39;\u4fe1\u53f7&#39;)<\/p>\n<p>plt.plot(time[peaks], signal[peaks], &#39;x&#39;, label=&#39;\u5c40\u90e8\u9ad8\u70b9&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u56fe\u50cf\u5904\u7406\u4e2d\u7684\u5c40\u90e8\u9ad8\u70b9<\/h4>\n<\/p>\n<p><p>\u5728\u56fe\u50cf\u5904\u7406\u9886\u57df\uff0c\u627e\u5230\u56fe\u50cf\u4e2d\u7684\u5c40\u90e8\u9ad8\u70b9\u53ef\u4ee5\u7528\u4e8e\u8fb9\u7f18\u68c0\u6d4b\u3001\u7279\u5f81\u70b9\u68c0\u6d4b\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u4f7f\u7528SciPy\u5e93\u627e\u5230\u56fe\u50cf\u4e2d\u7684\u5c40\u90e8\u9ad8\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from scipy.signal import find_peaks<\/p>\n<p>from skimage import data, color, filters<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u56fe\u50cf<\/strong><\/h2>\n<p>image = color.rgb2gray(data.astronaut())<\/p>\n<h2><strong>\u5e94\u7528\u8fb9\u7f18\u68c0\u6d4b<\/strong><\/h2>\n<p>edges = filters.sobel(image)<\/p>\n<h2><strong>\u627e\u5230\u56fe\u50cf\u4e2d\u6bcf\u884c\u7684\u5c40\u90e8\u9ad8\u70b9<\/strong><\/h2>\n<p>peaks = [find_peaks(row)[0] for row in edges]<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u50cf\u548c\u5c40\u90e8\u9ad8\u70b9<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<p>ax.imshow(edges, cmap=&#39;gray&#39;)<\/p>\n<p>for row_idx, row_peaks in enumerate(peaks):<\/p>\n<p>    ax.plot(row_peaks, np.ones_like(row_peaks) * row_idx, &#39;r.&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u91d1\u878d\u6570\u636e\u5206\u6790\u4e2d\u7684\u5c40\u90e8\u9ad8\u70b9<\/h4>\n<\/p>\n<p><p>\u5728\u91d1\u878d\u6570\u636e\u5206\u6790\u9886\u57df\uff0c\u627e\u5230\u80a1\u7968\u4ef7\u683c\u4e2d\u7684\u5c40\u90e8\u9ad8\u70b9\u53ef\u4ee5\u7528\u4e8e\u6280\u672f\u5206\u6790\u3001\u4ea4\u6613\u4fe1\u53f7\u751f\u6210\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u4f7f\u7528SciPy\u5e93\u627e\u5230\u80a1\u7968\u4ef7\u683c\u4e2d\u7684\u5c40\u90e8\u9ad8\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import pandas as pd<\/p>\n<p>from scipy.signal import find_peaks<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u80a1\u7968\u4ef7\u683c\u6570\u636e<\/strong><\/h2>\n<p>dates = pd.date_range(&#39;2020-01-01&#39;, &#39;2020-12-31&#39;)<\/p>\n<p>prices = np.sin(np.linspace(0, 10, len(dates))) + np.random.normal(0, 0.1, len(dates))<\/p>\n<h2><strong>\u4f7f\u7528find_peaks\u51fd\u6570\u627e\u5230\u5c40\u90e8\u9ad8\u70b9<\/strong><\/h2>\n<p>peaks, _ = find_peaks(prices)<\/p>\n<h2><strong>\u7ed8\u5236\u80a1\u7968\u4ef7\u683c\u548c\u5c40\u90e8\u9ad8\u70b9<\/strong><\/h2>\n<p>plt.plot(dates, prices, label=&#39;\u4ef7\u683c&#39;)<\/p>\n<p>plt.plot(dates[peaks], prices[peaks], &#39;x&#39;, label=&#39;\u5c40\u90e8\u9ad8\u70b9&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u5229\u7528SciPy\u5e93\u3001\u4f7f\u7528numpy\u6570\u7ec4\u3001\u7ed3\u5408\u4fe1\u53f7\u5904\u7406\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u627e\u5230\u6570\u636e\u4e2d\u7684\u5c40\u90e8\u9ad8\u70b9\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u627e\u5230\u5c40\u90e8\u9ad8\u70b9\u53ef\u4ee5\u7528\u4e8e\u8bb8\u591a\u9886\u57df\uff0c\u5982\u4fe1\u53f7\u5904\u7406\u3001\u56fe\u50cf\u5904\u7406\u3001\u91d1\u878d\u6570\u636e\u5206\u6790\u7b49\u3002\u901a\u8fc7\u8c03\u6574\u53c2\u6570\u548c\u5e94\u7528\u5e73\u6ed1\u3001\u6ee4\u6ce2\u7b49\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u627e\u5230\u5c40\u90e8\u9ad8\u70b9\u7684\u51c6\u786e\u6027\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5b9a\u4e49\u5c40\u90e8\u9ad8\u70b9\uff0c\u4f7f\u7528Python\u6c42\u89e3\u65f6\u9700\u8981\u8003\u8651\u54ea\u4e9b\u56e0\u7d20\uff1f<\/strong><br \/>\u5c40\u90e8\u9ad8\u70b9\u662f\u6307\u5728\u67d0\u4e2a\u7279\u5b9a\u533a\u57df\u5185\uff0c\u5176\u503c\u5927\u4e8e\u5468\u56f4\u90bb\u57df\u503c\u7684\u70b9\u3002\u5728\u4f7f\u7528Python\u8fdb\u884c\u6c42\u89e3\u65f6\uff0c\u9700\u8003\u8651\u6570\u636e\u7684\u7ef4\u5ea6\uff08\u5982\u4e00\u7ef4\u3001\u4e8c\u7ef4\u6216\u4e09\u7ef4\uff09\u3001\u6570\u636e\u7684\u5e73\u6ed1\u6027\u4ee5\u53ca\u5c40\u90e8\u9ad8\u70b9\u7684\u5b9a\u4e49\uff08\u5982\u90bb\u57df\u7684\u5927\u5c0f\uff09\u3002\u53ef\u4ee5\u4f7f\u7528NumPy\u6216SciPy\u7b49\u5e93\u6765\u5904\u7406\u6570\u636e\uff0c\u7ed3\u5408\u90bb\u57df\u641c\u7d22\u7684\u65b9\u6cd5\u8fdb\u884c\u5c40\u90e8\u9ad8\u70b9\u7684\u67e5\u627e\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u5bfb\u627e\u5c40\u90e8\u9ad8\u70b9\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\u6765\u5bfb\u627e\u5c40\u90e8\u9ad8\u70b9\u3002NumPy\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\uff0c\u9002\u5408\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\uff1bSciPy\u7684<code>signal<\/code>\u6a21\u5757\u5305\u542b\u4e86\u591a\u79cd\u4fe1\u53f7\u5904\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u5bfb\u627e\u5c40\u90e8\u6781\u503c\uff1bMatplotlib\u80fd\u591f\u5e2e\u52a9\u53ef\u89c6\u5316\u6570\u636e\uff0c\u4ece\u800c\u76f4\u89c2\u5c55\u793a\u5c40\u90e8\u9ad8\u70b9\u3002\u7ed3\u5408\u8fd9\u4e9b\u5e93\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u8fdb\u884c\u5c40\u90e8\u9ad8\u70b9\u7684\u68c0\u6d4b\u548c\u5206\u6790\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u566a\u58f0\u6570\u636e\u4ee5\u66f4\u51c6\u786e\u5730\u627e\u5230\u5c40\u90e8\u9ad8\u70b9\uff1f<\/strong><br \/>\u566a\u58f0\u6570\u636e\u53ef\u80fd\u4f1a\u5bfc\u81f4\u9519\u8bef\u7684\u5c40\u90e8\u9ad8\u70b9\u68c0\u6d4b\uff0c\u56e0\u6b64\u5728\u6c42\u89e3\u65f6\u53ef\u4ee5\u91c7\u7528\u6570\u636e\u5e73\u6ed1\u6280\u672f\uff0c\u6bd4\u5982\u79fb\u52a8\u5e73\u5747\u3001Gaussian\u5e73\u6ed1\u6216\u4e2d\u503c\u6ee4\u6ce2\u7b49\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u964d\u4f4e\u566a\u58f0\u5f71\u54cd\u540e\uff0c\u518d\u8fdb\u884c\u5c40\u90e8\u9ad8\u70b9\u7684\u68c0\u6d4b\uff0c\u53ef\u4ee5\u83b7\u5f97\u66f4\u51c6\u786e\u7684\u7ed3\u679c\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u8bbe\u7f6e\u9608\u503c\uff0c\u8fc7\u6ee4\u6389\u90a3\u4e9b\u5fae\u5c0f\u7684\u5c40\u90e8\u9ad8\u70b9\uff0c\u4ee5\u786e\u4fdd\u68c0\u6d4b\u7684\u9ad8\u70b9\u5177\u5907\u4e00\u5b9a\u7684\u610f\u4e49\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u7528Python\u6c42\u5c40\u90e8\u9ad8\u70b9 \u5229\u7528SciPy\u5e93\u3001\u4f7f\u7528numpy\u6570\u7ec4\u3001\u7ed3\u5408\u4fe1\u53f7\u5904\u7406\u65b9\u6cd5\uff0c\u8fd9\u4e9b\u662f\u7528Python\u6c42\u5c40 [&hellip;]","protected":false},"author":3,"featured_media":1174004,"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\/1173998"}],"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=1173998"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1173998\/revisions"}],"predecessor-version":[{"id":1174005,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1173998\/revisions\/1174005"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1174004"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1173998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1173998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1173998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}