반응형
STFT (Short Time Fourier Transform)
짧은 시간별로 FFT를 수행하여, 일정한 단위시간별 주파수 밀도를 스펙트로그램으로 표현
→ 시간에 따른 주파수 변화 확인 가능

시간해상도와 주파수해상도를 일정하게 유지 → Window Function 에 의해 고정
코드
라이브러리 import
mport pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal #science python : scipy
import math
주파수 f, 각주파수 w 정의
# 주파수 입력
f1 = 2
f2 = 8
f3 = 4
f4 = 6
f5 = 10
# w = 2pi*f
w1 = 2*np.pi*f1
w2 = 2*np.pi*f2
w3 = 2*np.pi*f3
w4 = 2*np.pi*f4
w5 = 2*np.pi*f5
이산함수 정의
# 시간열 생성
t = np.arange(0,10,0.005) # sampling freq : 200 Hz // sampling time : 10 seconds
x1 = np.concatenate([np.sin(w1*t), np.sin(w2*t), np.sin(w3*t), np.sin(w4*t), np.sin(w5*t)])
→ ((2000,), (10000,), (10000,), (10000,))
시각화
plt.figure(figsize=(15,12))
plt.subplot(2, 1, 1)
plt.plot(t_total, x1, 'r-', lw = 1)
plt.xlabel('t',fontsize=12)
plt.ylabel('x',fontsize=12)
plt.show()

STFT 변환
# 파라미터
Fs = 1/0.005 # 샘플링 주파수
f,t,Sxx = signal.spectrogram(x1, Fs, nperseg = 500, noverlap = 500//3)
f.shape, t.shape, Sxx.shape #주파수길이,시간,stft결과
시각화
plt.figure(figsize=(9,6))
plt.pcolormesh(t, f, Sxx, vmax = np.max(Sxx), cmap = 'hot', alpha = 0.7)
plt.ylim((0, 12))
plt.xlabel('Time(s)', fontsize=12)
plt.ylabel('Frequency(Hz)', fontsize=12)
plt.title('STFT Result', fontsize=20)
plt.colorbar()
plt.grid(alpha = 0.4)
plt.show()

결과

반응형
'개발기록 > python' 카테고리의 다른 글
[python] datetime 현재시간 구하기 시간비교 (0) | 2025.02.20 |
---|---|
[python] 주파수 신호 분석 방법론 3 - Wavelet 변환 (0) | 2024.11.26 |
[python] 주파수 신호 분석 방법론 1 - FFT (Fast Fourier Transform) (0) | 2024.11.24 |
[python] selenium-stealth 셀레늄 감지 방지 (구글 크롬) (2) | 2024.11.15 |
구글 코랩(Colab) 사용하기 (+구글 드라이브 연결, 압축풀기) (0) | 2024.11.07 |
댓글