---
# System prepended metadata

title: 波形與頻譜
tags: [soundscape viewer, ' audio processing', ' tutorial', ' ecoacoustics']

---

---
title: 波形與頻譜
description: waveform and spectrogram
tags: soundscape viewer, tutorial, ecoacoustics, audio processing
disqus: hackmd
---

# 波形圖、頻譜圖

在分析單筆聲音檔案時，經常會使用波形圖 (waveform) 和頻譜圖 (spectrogram) 來觀察聲音在時域和頻率上的變化趨勢。Soundscape Viewer 也提供了 **audio_visualization** 這個小工具可以讓使用者快速的觀察波形和頻譜。

## 頻譜分析參數

在分析頻譜的時候，必須要指定快速傅立葉變換 (Fast Fourier transform) 的視窗長度 (*fft_size*) 和視窗之間的重疊比例 (*window_overlap*)。此外，若是只想呈現一小段時間的聲音，則可以指定開始時間 (*offset_read*) 和讀取長度 (*duration_read*) 的秒數。

```python=
# import audio_visualization
from soundscape_IR.soundscape_viewer import audio_visualization

# display the waveform and spectrogram
audio_visualization(filename='example.wav', offset_read=5,...
                    duration_read=10, fft_size=512, window_overlap=0.5)
```

## 分析 Drive 資料夾上的聲音檔案

如果聲音檔案在 Drive 資料夾上，也可以運用 Soundscape Viewer 的 Drive 連結功能，下載聲音檔案到本地資料夾，並分析其波形圖和頻譜圖。

```python
# import lts_maker & audio_visualization
from soundscape_IR.soundscape_viewer import lts_maker
from soundscape_IR.soundscape_viewer import audio_visualization


# Get a list of the audio files from the designated Drive folder
folder_id='1_ZQzFFN6tJmcDS7McOdPZUhnuZVBP8Ub' # Define the folder ID
LTS_run.collect_Gdrive(folder_id)

# Download the audio data
file_no=0 # The number of audio file (integral, start from 0)
temp=LTS_run.Gdrive.file_list[file_no]
temp.GetContentFile(temp['title'])
audio_visualization(LTS_run.audioname[file_no], duration_read=None,...
                    fft_size=512, window_overlap=0.5)
```

## 觀察聲音波形與頻譜特性
如果一切順利，應該就會看到這張圖，上面為聲音的波形圖，下面則為聲音的頻譜圖。

![waveform and spectrogram](https://lh3.googleusercontent.com/pw/ACtC-3dgQoFMsqkaTzc8d7-Xao8P139r1FUPz7P2Pdu7x8_4EzfsSrKyNvicQnK3uumSXSdVj4fxKzXPKeKh_8fv3kTknxo4xO5v-QBFlgVfdIrq9a5POaa3i1t-pE0D2fj2NJhsth6hFKYcugYFih0hBJu3=w847-h589-no?authuser=0)
