Engee 中的数字信号处理 (DSP) 入门
使用函数
信号加载和可视化
使用 "函数可让用户使用列出的模块,类似于其他语言中的 "导入 "函数:
多个输出不必像在 MATLAB 中那样用括号括起来。 |
using DSP, WAV, Plots
plotly()
s, fs = wavread("/user/test.wav")
plot(0:1/fs:(length(s)-1)/fs, s)
输出
下面的函数使用带有语音标准参数(汉宁窗 `25 ms,重叠 `10 ms)的频谱图,构建并返回频谱图:
S = spectrogram(s[:,1], convert(Int, 25e-3*fs), convert(Int, 10e-3*fs); window=hanning)
Plots.heatmap(S.time.*1000, S.freq, pow2db.(S.power), xguide = "Время, мс", yguide = "Частота, Гц")
输出
信号处理
现在,让我们将信号通过一个滤波器来模拟手机的带宽,并再次绘制其频谱图:
responsetype = Bandpass(300, 3400; fs=fs)
prototype = Butterworth(8)
telephone_filter = digitalfilter(responsetype, prototype)
让我们来看看滤波器的特性:
变量可以使用 Unicode 名称。输入法为 \omega + tab。 |
ω = 0:0.01:pi
H = freqz(telephone_filter, ω)
过滤信号
sf = filt(telephone_filter, s)
Sf = spectrogram(s[:,1], convert(Int, 25e-3*fs), convert(Int, 10e-3*fs); window=hanning)
Plots.heatmap(Sf.time.*1000, Sf.freq, pow2db.(Sf.power), xguide = "Время, мс", yguide = "Частота, Гц")
输出
成果复制
using Base64
function audioplayer(filepath)
markup = """<audio controls="controls" {autoplay}>
<source src="$filepath" />
Your browser does not support the audio element.
</audio>"""
display(MIME("text/html") ,markup)
end
function audioplayer(s, fs)
buf = IOBuffer()
wavwrite(s, buf; Fs=fs)
data = base64encode(unsafe_string(pointer(buf.data), buf.size))
markup = """<audio controls="controls" {autoplay}>
<source src="data:audio/wav;base64,$data" type="audio/wav" />
Your browser does not support the audio element.
</audio>"""
display(MIME("text/html") ,markup)
end
audioplayer(s, fs)
audioplayer(sf, fs)
输出
信号超采样
在本例中,我们将使用低通滤波器设计,将音频信号从 "48kHz "重采样到 "44.1kHz",然后对信号进行重采样。
让我们找出插值和抽取系数。
using Plots
using DSP
Fdat = 48e3;
Fcd = 44.1e3;
LM = Rational{Int32}(Fcd/Fdat);
L = LM.num;
M = LM.den;
(L,M)
输出
(147, 160)
可视化原始音频信号
t = 0:1/Fdat:0.25-1/Fdat;
x = sin.(2*pi*1.5e3*t);
gr()
plot(t, x, line=:stem, marker=:circle)
xlims!((0,0.001))
输出
让我们对获得的信号重新采样并叠加到原始信号上
f = (Fdat/2)*min(1/L,1/M)
win = DSP.Windows.kaiser(3579,3);
fir = DSP.Filters.digitalfilter(DSP.Filters.Lowpass(f/48e3),FIRWindow(win)).
xup = DSP.Filters.resample(x,Int64(L),fir);
y = L.*xup[1:M:end].
t_res = (0:(length(y)-1))/Fcd;
plot!(t_res, y, line=:stem, marker=:circle)