chainerRLを動かす
python 数値計算 平方根
# -*- coding: utf-8 -*- import math def main(): x = 5 y = math.sqrt(x) print(y) if __name__ == "__main__": main()
python数値計算 5のべき乗を計算したい場合
pythonでべき乗
5のべき乗を計算したい場合
# -*- coding: utf-8 -*- def main(): x = 5 y = pow(x,2) print(y) if __name__ == "__main__": main()
python 数値計算 絶対値|a|
pythonで絶対値を計算したい場合
abs(またはabsolute等)を使う
# -*- coding: utf-8 -*- def main(): a = -10 a = abs(a) print(a) if __name__ == "__main__": main()
python 数値計算 4捨5入
1.25を入力した場合round(x,1)で少数第一位で四捨五入する 以下コード
#coding: utf-8 def main(): x = 1.23 x = round(x,1) print(x) if __name__ == "__main__": main()
FIRフィルタの設計by python
FIRフィルタの設計by python
○firwinの使い方
例)
scipy.signal.firwin(numtaps, cutoff, width=None, window='hamming', pass_zero=True, scale=True, nyq=1.0)
numtaps:タップ数
cutoff:カットオフ周波数
window:窓関数
pass_zero:
nyq:ナイキスト周波数
参考:https://docs.scipy.org/doc/scipy-0.13.0/reference/generated/scipy.signal.firwin.html
○scipy.absoluteの使い方
NumPy で絶対値 (負の数は正の数として出力、正の数は正の数として出力) を求めるには、np.absolute()があります。
参考https://docs.scipy.org/doc/numpy-1.9.1/reference/generated/numpy.absolute.html
○scipy.fft使い方
https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.fftpack.fft.html
○unwrapとは
連続だったデータもラップ(wrap)されて位相のジャンプが現れる。 これをUnwrapするたのもの
参考retrofocus28.blogspot.jp
#coding:utf8 import scipy import scipy.signal as ss from matplotlib import pyplot as pp import numpy as np import scipy.signal from pylab import * N = 221 fc = 1000. fc2 = 3000. Fs = 44100. #%% LPF h = ss.firwin(numtaps=N, cutoff=fc/(Fs/2.), window='blackman', pass_zero=True) #%% HPF f = ss.firwin(numtaps=N, cutoff=fc/(Fs/2.), window='blackman', pass_zero=False) #%% BPF e = ss.firwin(numtaps=N, cutoff=scipy.array([fc/(Fs/2.), fc2/(Fs/2.)]), window='blackman', pass_zero=False) #%% BEF h = ss.firwin(numtaps=N, cutoff=scipy.array([fc/(Fs/2.), fc2/(Fs/2.)]), window='blackman', pass_zero=True) #%% 表示 f = scipy.array(range(0, N)) * Fs / scipy.double(N) tf = scipy.fft(h) mag = scipy.absolute(tf) phase = scipy.unwrap(scipy.angle(tf)) * 180. / scipy.pi figure(1) subplot(3,1,1) pp.plot(h) grid('on', 'both') subplot(3,1,2) pp.semilogx(f, mag) xlim([20, 20000]) ylim([0, 1.2]) grid('on', 'both') subplot(3,1,3) pp.plot(f, phase) xlim([0, 20000]) grid('on', 'both') figure(2) subplot(3,1,1) pp.plot(f) grid('on', 'both') subplot(3,1,2) pp.semilogx(f, mag) xlim([20, 20000]) ylim([0, 1.2]) grid('on', 'both') subplot(3,1,3) pp.plot(f, phase) xlim([0, 20000]) grid('on', 'both') figure(3) subplot(3,1,1) pp.plot(e) grid('on', 'both') subplot(3,1,2) pp.semilogx(f, mag) xlim([20, 20000]) ylim([0, 1.2]) grid('on', 'both') subplot(3,1,3) pp.plot(f, phase) xlim([0, 20000]) grid('on', 'both') plt.show() || 参考