Python의 Matplotlib.pyplot.specgram()

  1. matplotlib.pyplot.specgram()메서드를 사용하여 스펙트로 그램 플로팅
  2. scipy.signal.spectrogram()메서드를 사용하여 스펙트로 그램 플로팅
Python의 Matplotlib.pyplot.specgram()

이 튜토리얼에서는matplotlib.pyplot.specgram()scipy.signal.spectrogram()메서드를 사용하여 Python에서 스펙트로 그램을 그리는 방법을 설명합니다.

스펙트로 그램을 사용하여 신호의 강도에 대한 세부 정보를 얻을 수 있습니다. 특정 지점에서 스펙트로 그램의 색상이 어두울수록 해당 지점의 신호가 더 강해집니다.

matplotlib.pyplot.specgram()메서드를 사용하여 스펙트로 그램 플로팅

matplotlib.pyplot.specgram(x,
                           NFFT=None,
                           Fs=None,
                           Fc=None,
                           detrend=None,
                           window=None,
                           noverlap=None,
                           cmap=None,
                           xextent=None,
                           pad_to=None,
                           sides=None,
                           scale_by_freq=None,
                           mode=None,
                           scale=None,
                           vmin=None,
                           vmax=None, *,
                           data=None,
                           **kwargs)

예: matplotlib.pyplot.specgram()메서드를 사용하여 스펙트로 그램 플로팅

import math
import numpy as np
import matplotlib.pyplot as plt

dt = 0.0001
w = 2
t = np.linspace(0, 5, math.ceil(5 / dt))
A = 20 * (np.sin(3 * np.pi * t))

plt.specgram(A, Fs=1)
plt.title("Spectrogram Using matplotlib.pyplot.specgram() method")
plt.show()

출력:

matplotlib.pyplot.specgram() 메서드를 사용하여 스펙트로 그램 플로팅

matplotlib.pyplot.specgram()메서드를 사용하여A=20sin(3*np.pi*t)함수에 대한 스펙트로 그램을 생성합니다. 메서드의 인수 fs는 샘플링 빈도를 나타냅니다.

scipy.signal.spectrogram()메서드를 사용하여 스펙트로 그램 플로팅

import math
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal

dt = 0.0001
w = 2
t = np.linspace(0, 5, math.ceil(5 / dt))
A = 2 * (np.sin(1 * np.pi * 300 * t))

f, t, Sxx = signal.spectrogram(A, fs=1, nfft=514)
plt.pcolormesh(t, f, Sxx)
plt.ylabel("Frequency")
plt.xlabel("Time")
plt.title("Spectrogram Using scipy.signal.spectrogram() method")
plt.show()

출력:

scipy.signal.spectrogram() 메서드를 사용한 스펙트로 그램

scipy.signal.spectrogram()메서드를 사용하여A=2sin(300*np.pi*t)함수에 대한 스펙트로 그램을 생성합니다. 메소드의 인수fs는 샘플링 주파수를 나타내고ntft는 사용 된FFT의 길이를 나타냅니다.

이 메서드는 f, tSxx세 가지 값을 반환합니다. f는 샘플 주파수의 배열을, t는 샘플 시간의 배열을, SxxA의 스펙트로 그램을 나타냅니다.

이 방법은 입력 신호의 스펙트로 그램 플롯을 생성하지 않습니다. matplotlib.pyplot.colormesh()를 사용하여 Figure를 생성 할 수 있습니다.

튜토리얼이 마음에 드시나요? DelftStack을 구독하세요 YouTube에서 저희가 더 많은 고품질 비디오 가이드를 제작할 수 있도록 지원해주세요. 구독하다
작가: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

Suraj Joshi is a backend software engineer at Matrice.ai.

LinkedIn