serial通信で制御設定を変える
serial通信でポート制御設定を変える
// L->H(2sec)->L(val)->H(2sec) #define LED_PIN 13 int blinkDelay = 500; int LEDState = LOW; int data = 1000; void setup() { Serial.begin(115200); Serial.setTimeout(10); Serial.println("start"); pinMode(LED_PIN, OUTPUT); } void loop() { if (Serial.available() > 0) { data = Serial.parseInt(); if ((data >= 100) && (data <= 4000)) { blinkDelay = data; } } // if (LEDState == LOW) { // LEDState = HIGH; // } // else { // LEDState = LOW; // } Serial.println(blinkDelay); digitalWrite(LED_PIN, HIGH); delay(2000); digitalWrite(LED_PIN, LOW); delay(blinkDelay); blinkDelay +=100; }
matplot tips 色指定 fill_between stem
検索: fill_between matplot sabopy.com
matplotlib.collections — Matplotlib 3.6.0 documentation
# https://helve-blog.com/posts/python/numpy-fast-fourier-transform/ # https://sabopy.com/py/matplotlib-110/ import numpy as np import matplotlib.pyplot as plt N = 1024 # サンプル数 dt = 0.001 # サンプリング周期 [s] f1, f2 = 50, 120 # 周波数 [Hz] def func(t): return 1.5*np.sin(2*np.pi*f1*t) + np.sin(2*np.pi*f2*t) + 3 # 信号 t = np.arange(0, N*dt, dt) # 時間 [s] print(t) #x = 1.5*np.sin(2*np.pi*f1*t) + np.sin(2*np.pi*f2*t) + 3 # 信号 x =func(t) fig, ax = plt.subplots() ax.plot(t, x) # ax.set_xlim(0, 0.1) ax.set_xlabel("Time [s]") ax.set_ylabel("Signal") ax.grid() #t_ = np.arange(0.04, 0.6, dt) #ax.fill_between(t_,func(t_),fc="0.8",ec="0.5") plt.show() F = np.fft.fft(x) # 変換結果 freq = np.fft.fftfreq(N, d=dt) # 周波数 fig, ax = plt.subplots(nrows=3, sharex=True, figsize=(6,6)) ax[0].plot(F.real, label="Real part") ax[0].legend() ax[1].plot(F.imag, label="Imaginary part") ax[1].legend() ax[2].plot(freq, label="Frequency") ax[2].legend() ax[2].set_xlabel("Number of data") plt.show() Amp = np.abs(F/(N/2)) # 振幅 fig, ax = plt.subplots() ax.plot(freq[1:int(N/2)], Amp[1:int(N/2)]) ax.set_xlabel("Freqency [Hz]") ax.set_ylabel("Amplitude") ax.grid() #ax.fill_between(freq[1:int(N/2)],Amp[1:int(N/2)]) ax.fill_between(freq[5:50],Amp[5:50],fc="tab:blue") plt.show()
もしくはstemをつかう。
VTF (dooley)
import math import numpy as np import matplotlib.pyplot as plt #vtf = 5.05*math.exp(-0.138 * math.pi*300/180 * u ) * ( 1 - math.exp(-0.1 * math.pi * 300/180 * u) ) u = np.linspace(0, 10, 101) #print(u) #print(u.size) vtf = np.empty(u.size) for i in range(u.size): vtf[i] = 5.05*math.exp(-0.138*math.pi*300/180*u[i])*(1-math.exp(-0.1*math.pi*300/180*u[i])) #print(vtf) plt.plot(u, vtf) plt.show()
FFT 参考
import numpy as np import matplotlib.pyplot as plt N = 1024 # サンプル数 dt = 0.001 # サンプリング周期 [s] f1, f2 = 50, 120 # 周波数 [Hz] t = np.arange(0, N*dt, dt) # 時間 [s] x = 1.5*np.sin(2*np.pi*f1*t) + np.sin(2*np.pi*f2*t) + 3 # 信号 fig, ax = plt.subplots() ax.plot(t, x) # ax.set_xlim(0, 0.1) ax.set_xlabel("Time [s]") ax.set_ylabel("Signal") ax.grid() plt.show() F = np.fft.fft(x) # 変換結果 freq = np.fft.fftfreq(N, d=dt) # 周波数 fig, ax = plt.subplots(nrows=3, sharex=True, figsize=(6,6)) ax[0].plot(F.real, label="Real part") ax[0].legend() ax[1].plot(F.imag, label="Imaginary part") ax[1].legend() ax[2].plot(freq, label="Frequency") ax[2].legend() ax[2].set_xlabel("Number of data") plt.show() Amp = np.abs(F/(N/2)) # 振幅 fig, ax = plt.subplots() ax.plot(freq[1:int(N/2)], Amp[1:int(N/2)]) ax.set_xlabel("Freqency [Hz]") ax.set_ylabel("Amplitude") ax.grid() plt.show()