/
spectrogram.jl
134 lines (116 loc) · 3.4 KB
/
spectrogram.jl
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import DSP.Periodograms:
PSDOnly,
nextfastfft,
Spectrogram,
spectrogram,
stft,
fft2pow!,
fft2oneortwosided!,
compute_window,
arraysplit,
stfttype
function DSP.spectrogram(
s::AbstractVector{T},
estimator::Function,
n::Int = length(s) >> 3,
noverlap::Int = n >> 1;
onesided::Bool = eltype(s) <: Real,
nfft::Int = nextfastfft(n),
fs::Real = 1,
window::Union{Function,AbstractVector,Nothing} = nothing,
) where {T}
out = stft(
s,
estimator,
n,
noverlap,
PSDOnly();
onesided = onesided,
nfft = nfft,
fs = fs,
window = window,
)
Spectrogram(
out,
onesided ? DSP.rfftfreq(nfft, fs) : DSP.fftfreq(nfft, fs),
(n/2:n-noverlap:(size(out, 2)-1)*(n-noverlap)+n/2) / fs,
)
end
"""
stft(s::AbstractVector{T}, estimator::Function, n::Int = length(s) >> 3, noverlap::Int = n >> 1, psdonly::Union{Nothing, PSDOnly} = nothing; onesided::Bool = eltype(s) <: Real, nfft::Int = nextfastfft(n), fs::Real = 1, window::Union{Function, AbstractVector, Nothing} = nothing)
Model-based short-time Fourier transform.
# Arguments:
- `s`: Signal
- `estimator`: DESCRIPTION
- `n`: DESCRIPTION
- `noverlap`: DESCRIPTION
- `psdonly`: DESCRIPTION
- `onesided`: DESCRIPTION
- `nfft`: DESCRIPTION
- `fs`: DESCRIPTION
- `window`: DESCRIPTION
"""
function stft(
s::AbstractVector{T},
estimator::Function,
n::Int = length(s) >> 3,
noverlap::Int = n >> 1,
psdonly::Union{Nothing,PSDOnly} = nothing;
onesided::Bool = eltype(s) <: Real,
nfft::Int = nextfastfft(n),
fs::Real = 1,
window::Union{Function,AbstractVector,Nothing} = nothing,
) where {T}
win, norm2 = compute_window(window, n)
sig_split = arraysplit(s, n, noverlap, nfft, win)
nout = onesided ? (nfft >> 1) + 1 : nfft
out = zeros(stfttype(T, psdonly), nout, length(sig_split))
freqs = onesided ? DSP.rfftfreq(nfft, fs) : DSP.fftfreq(nfft, fs)
r = fs * norm2 / sqrt(length(freqs))
offset = 0
for (i, sig) in enumerate(sig_split)
# mul!(tmp, plan, sig)
tmp = estimator(sig, freqs)
if isa(psdonly, PSDOnly)
fft2pow!(out, tmp, nfft, r, onesided, offset)
else
fft2oneortwosided!(out, tmp, nfft, onesided, offset)
end
offset += nout
end
out
end
"""
model_spectrum(f, h, args...; kwargs...)
# Arguments:
- `f`: the model-estimation function, e.g., `ar,arma`
- `h`: The sample time
- `args`: arguments to `f`
- `kwargs`: keyword arguments to `f`
# Example:
```
using ControlSystemIdentification, DSP
T = 1000
s = sin.((1:T) .* 2pi/10)
S1 = spectrogram(s,window=hanning)
estimator = model_spectrum(ar,1,2)
S2 = spectrogram(s,estimator,window=rect)
plot(plot(S1),plot(S2)) # Requires the package LPVSpectral.jl
```
"""
function model_spectrum(f, h, args...; kwargs...)
function (s::AbstractArray{T}, freqs) where {T}
d = iddata(s, h)
model = f(d, args...; kwargs...)
tmp = vec(Complex{T}.(freqresp(model, T(2pi) .* freqs)))
end
end
# function rootspectrogram(model, fs)
# roots = map(1:length(model)) do i
# sys = tf(1,[1;-reverse(model.θ[:,i])],1)
# (sort(poles(sys), by=imag, rev=true)[1:end÷2])
# # log.(sort(complex.(eigvals(model.At[:,:,i])), by=imag, rev=true)[1:end÷2])
# end
# S = reduce(hcat,roots)
# fs/(2pi) .* angle.(S)'
# end