-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspectrogram.R
More file actions
79 lines (59 loc) · 1.5 KB
/
spectrogram.R
File metadata and controls
79 lines (59 loc) · 1.5 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#install.packages("tuneR")
library(tuneR)
# define path to audio file
fin = 'C:/Adele - Rolling In The Deep.mp3'
# read in audio file
data = readMP3(fin)
# extract signal
snd = data@left
# determine duration
dur = length(snd)/data@samp.rate
dur # seconds
# [1] 228.3624
# determine sample rate
fs = data@samp.rate
fs # Hz
## [1] 44100
#######################################################################################
#Plot wave
# demean to remove DC offset
snd = snd - mean(snd)
# plot waveform
plot(snd, type = 'l', xlab = 'Samples', ylab = 'Amplitude')
#######################################################################################
#Plot spectrogram
# number of points to use for the fft
nfft=1024
# window size (in points)
window=256
# overlap (in points)
overlap=128
#install.packages("signal")
#install.packages("oce")
library(signal, warn.conflicts = F, quietly = T) # signal processing functions
library(oce, warn.conflicts = F, quietly = T) # image plotting functions and nice color maps
# create spectrogram
spec = specgram(x = snd,
n = nfft,
Fs = fs,
window = window,
overlap = overlap
)
# discard phase information
P = abs(spec$S)
# normalize
P = P/max(P)
# convert to dB
P = 10*log10(P)
# config time axis
t = spec$t
# plot spectrogram
imagep(x = t,
y = spec$f,
z = t(P),
col = oce.colorsViridis,
ylab = 'Frequency [Hz]',
xlab = 'Time [s]',
drawPalette = T,
decimate = F
)