Getting Started with Digital Signal Processing (DSP) in Engee
Working with functions
Loading and visualisation of the signal
The using
function makes the listed modules available to the user, similar to the import
function in other languages:
Multiple outputs do not have to be bracketed as in MATLAB. |
using DSP, WAV, Plots
plotly()
s, fs = wavread("/user/test.wav")
plot(0:1/fs:(length(s)-1)/fs, s)
Output
The function below takes a spectrogram with standard parameters for speech (Hanning windows 25 ms
, overlap 10 ms
), constructs and returns a spectrogram:
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 = "Частота, Гц")
Output
Signal processing
Now let’s pass the signal through a filter to simulate the phone’s bandwidth, and plot its spectrogram again:
responsetype = Bandpass(300, 3400; fs=fs)
prototype = Butterworth(8)
telephone_filter = digitalfilter(responsetype, prototype)
Let’s look at the filter characteristic:
Variables can have Unicode names. This is typed as \omega + tab. |
ω = 0:0.01:pi
H = freqz(telephone_filter, ω)
Filter our signal:
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 = "Частота, Гц")
Output
Reproduction of results
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)
Output
Signal oversampling
In this example we will look at resampling an audio signal from 48kHz
to 44.1kHz
using a low pass filter design followed by resampling of the signal.
Let’s find the interpolation and decimation coefficients.
using Plots
using DSP
Fdat = 48e3;
Fcd = 44.1e3;
LM = Rational{Int32}(Fcd/Fdat);
L = LM.num;
M = LM.den;
(L,M)
Output
(147, 160)
Visualise the original audio signal:
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))
Output
Let’s resample and overlay the obtained signal on the original one
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)