File size: 1,153 Bytes
bee4911
 
ddc4c97
 
 
 
 
 
 
bee4911
ddc4c97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3091cbe
 
ddc4c97
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
---
license: cc-by-sa-4.0
language:
- en
tags:
- music
- spectrogram
size_categories:
- n<1K
---

## Google/MusicCapsをスペクトログラムにしたデータ。

### データ作った方法
```python
from PIL import Image
import IPython.display
import cv2

# 1. wavファイルを解析
y, sr = librosa.load("wavファイルなど")

# 2. フーリエ変換を適用して周波数成分を取得
D = librosa.amplitude_to_db(np.abs(librosa.stft(y)), ref=np.max) # librosaを用いてデータを作る
image = Image.fromarray(np.uint8(D), mode='L')  # 'L'は1チャンネルのグレースケールモードを指定します
image.save('spectrogram_{}.png')
```

### ♪復元方法
```python
im = Image.open("pngファイル")
db_ud = np.uint8(np.array(im))
amp = librosa.db_to_amplitude(db_ud)
print(amp.shape)
# (1025, 861)は20秒のwavファイルをスペクトログラムにした場合
# (1025, 431)は10秒のwavファイルをスペクトログラムにした場合
# (1025, 216)は5秒のwavファイルをスペクトログラムにした場合

y_inv = librosa.griffinlim(amp*200)
display(IPython.display.Audio(y_inv, rate=sr))
```