Time domain analysis and processing with EngeeDSP
We are getting acquainted with the functionality of EngeeDSP using the example of time domain signal analysis and processing.
Time domain signal analysis and processing is an approach to working with signals in which we consider its basic parameters that change over time. The analysis helps to extract important numerical characteristics from the "raw" signal.:
- 
Amplitude analysis: peak value, average value (constant component), RMS value, etc. 
- 
Time analysis: signal duration, pulse repetition rate, front rise rate. 
- 
Waveform: distortion analysis, presence of "spikes" (outliers), attenuation, etc. 
Processing allows you to extract information from a signal, change its characteristics and statistical indicators, scale it by level or smooth its shape.
The purpose of this example is to visualize the signal recorded from a real sensor, perform preprocessing, evaluate statistical metrics, local extremes, and cut off unwanted outliers.
Signal import and visualization
The data from the sensor's ADC is stored in a file data.txt. We consider them as a matrix in a variable datamat:
using DelimitedFiles
datamat = readdlm("data.txt")
Let's extract separate vectors for time and "raw" values from it, and display them on a graph in the time domain.:
t = datamat[:,1];
original = datamat[:,2];
plot(t, original, xguide = "Время (с)", title = "Исходный сигнал", legend = false)
Removing the permanent component
The analysis of statistical metrics can be hindered by the "drift" of the signal - a smooth change in its constant component with relatively fast oscillations around it.
Let's get rid of the constant component using the function detrend from the set of functions EngeeDSP. Under the hood of the function, a method is used to approximate the signal values by a polynomial of an arbitrary order, and then the calculated polynomial is subtracted from the signal. In our case, a 12th-order polynomial is suitable.:
sig = EngeeDSP.Functions.detrend(original, 12)
plot(t, sig, xguide = "Время (с)", title = "Сигнал без постоянной составляющей", legend = false)
Signal Statistics
Now that the signal is oscillating around zero, we can calculate the statistics we are interested in.
Consider the EngeeDSP functions for determining such "popular" signal metrics as extremes, RMS, and variance. But first, let's estimate the sampling period of the signal.:
dt = EngeeDSP.Functions.mean(diff(t))
And the sampling rate of the signal:
fs = 1/dt
Let's define the duration of the discrete signal - the number of samples in the vector sig:
nsamples = length(sig)
Let's find the maximum value and the ordinal number of the maximum reference in the numerical vector:
maxval, maxidx = EngeeDSP.Functions.max(sig)
The same is true for the minimum value. These are the absolute extremes of the signal:
minval, minidx = EngeeDSP.Functions.min(sig)
Knowing them, you can determine the magnitude of the signal.:
range = abs(maxval - minval)
Now let's calculate the arithmetic mean (note that it is close to zero in value):
EngeeDSP.Functions.mean(sig)
Let's find the RMS value:
rmsval = EngeeDSP.Functions.rms(sig)
And the variance:
EngeeDSP.Functions.var(sig)
Signal peak analysis
It is often useful to be able to find local signal extremes, or so-called peaks. To do this, use the function findpeaks. The function returns the values and indices of peaks, as well as (optionally) their width and severity. As additional input arguments, specify the minimum peak height and the minimum distance between neighboring peaks.:
pks, locs, w, p = EngeeDSP.Functions.findpeaks(sig, out=:data, 
                                            MinPeakHeight = 0.08,
                                            MinPeakDistance = 500);
plot(t,sig, xguide = "Время (с)", label = false)
scatter!(t[locs], pks, label = "Пики")
We will output the amplitudes and their corresponding time points programmatically. We will also output the width and severity:
hcat(t[locs], pks, w, p)
Running Median Filter
In the time domain, you can apply basic operations of averaging or trimming the signal using simple sliding window filters - averaging, median, and so on.
Let's get rid of the peaks using a nonlinear filter, namely, a running median with a window of fifteen samples. To do this, use the function movmedian:
nospikes = EngeeDSP.Functions.movmedian(sig, 15);
plot(t,sig, xguide = "Время (с)", label = "До фильтра")
plot!(t, nospikes, linewidth = 2.5, label = "После фильтра")
Let's check the statistical metrics of the filtered signal:
EngeeDSP.Functions.rms(nospikes)
EngeeDSP.Functions.var(nospikes)
newmax, maxidx = EngeeDSP.Functions.max(nospikes);
newmin, minidx = EngeeDSP.Functions.min(nospikes);
new_range = abs(newmax - newmin)
Conclusion
We got acquainted with the EngeeDSP functionality for tasks of visualization, analysis and processing of signals in the time domain. Using the described functions, you can:
- 
Measure the basic parameters of the signal (amplitude, duration). 
- 
Visualize the waveform. 
- 
Clean the signal from noise and interference using simple operations (averaging, filtering). 
- 
Prepare the signal for deeper analysis, for example, to transition to the frequency domain using the Fourier transform.