Я использую следующие устройства:
- Raspberry PI 5
- Акселерометр Adafruit ISM330DHCX
Код: Выделить всё
def read_sensor():
global data
target_duration = 1 / SAMPLE_RATE
# Connect to sensor
...
while True:
loop_start_time = time.perf_counter()
x, y, z = accelerometer.acceleration
acceleration = np.sqrt(x**2 + y**2 + z**2)
data = np.roll(data, -1)
data[-1] = acceleration
# Try to get exactly SAMPLE_RATE per second
loop_duration = time.perf_counter() - loop_start_time
sleep_time = max(0, target_duration - loop_duration)
end_time = time.perf_counter() + sleep_time
while time.perf_counter() < end_time: pass
Код: Выделить всё
n = len(data)
fft = abs(np.fft.fft(data * np.blackman(n)))[:n//2]
amplitudes = np.abs(fft) / n
# Identify max frequencies
max_amplitude = np.max(amplitudes)
max_index = np.argmax(amplitudes)
frequencies = np.fft.fftfreq(n, d=1/n)[:n//2]
# Generate summary
result = {
'mean': round(data.mean(), 4),
'std': round(data.std(), 4),
'max': round(data.max(), 4),
'min': round(data.min(), 4),
'range': round(data.max() - data.min(), 4),
'fft_mean': round(np.mean(fft), 4),
'fft_std': round(np.std(fft), 4),
'max_freq': round(frequencies[max_index], 4),
'max_amp': round(max_amplitude, 4),
'speed': duty_cycle
}
Код: Выделить всё
mean,std,max,min,range,fft_mean,fft_std,max_freq,max_amp,speed
9.9587,0.1261,10.599,9.5467,1.0523,15.6502,308.3268,0.0,4.1798,47
9.9609,0.1287,10.599,9.5467,1.0523,16.2121,308.5345,0.0,4.1826,47
Мне известна теорема о частоте Найквиста, согласно которой я могу обнаруживать только частоты ниже SAMPLE_RATE/2 Гц.
Мои вопросы следующие:
- Почему не работает ускорение так сильно меняется, когда я добавляю кусок пластика?
- Почему мое БПФ всегда показывает частоту 0,0?
- Где я допустил ошибку в своем код ?
Подробнее здесь: https://stackoverflow.com/questions/790 ... meter-data