前提・実現したいこと
python3.6のScipyライブラリを使って、あるデータの極値を求めようと考えています。
argrelmax()、argrelmin()という関数を使えば、Numpy配列から作ったグラフの極値のインデックスを取得できることを知ったのですが、そこから極値に該当するデータのみをNumpy配列やリストに収める方法がわからず、頭を抱えています。
ソースコードの
e_center.txt
には、改行で区切られたグラフの値(x-y座標でいうところのyの値のみ)が入っています。
発生している問題・エラーメッセージ
C:\Users\USER\python\FDTD\シン・ELECTROMAGNETIC SIMULATION USING THE FDTD(202201 10より)\テストコード>python for_envelope.py Traceback (most recent call last): File "for_envelope.py", line 22, in <module> maxid = signal.argrelmax(e_center_raw_n, order=1) File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\s cipy\signal\_peak_finding.py", line 191, in argrelmax return argrelextrema(data, np.greater, axis, order, mode) File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\s cipy\signal\_peak_finding.py", line 246, in argrelextrema axis, order, mode) File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\s cipy\signal\_peak_finding.py", line 74, in _boolrelextrema results &= comparator(main, plus) numpy.core._exceptions.UFuncTypeError: ufunc 'greater' did not contain a loop wi th signature matching types (dtype('<U23'), dtype('<U23')) -> dtype('bool')
該当のソースコード
python3.6
1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3 4""" 5包絡線を書く用 6""" 7 8import numpy as np 9import scipy.constants as sc 10from scipy import signal 11 12 13with open('e_center.txt') as f: 14 e_center_raw = [s.strip() for s in f.readlines()] 15 16e_center_raw_n = np.array(e_center_raw) 17 18maxid = signal.argrelmax(e_center_raw_n, order=1) 19minid = signal.argrelmin(e_center_raw_n, order=1) 20 21e_center_env = e_center_raw_n[maxid] 22 23f = open('e_center_envelope.txt', 'w') 24for x in e_center_env: 25 f.write(str(x) + "\n") 26f.close()
補足情報(FW/ツールのバージョンなど)
scipy 1.5.4
numpy 1.19.5

回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2022/01/16 12:53