void EnvelopeDetector(TVec Src, TVec Dst, Int32 FrameSize)
Fast envelope detector.
| # | Name | Type | Description |
|---|---|---|---|
| 1 | Src | TVec | source TVec |
| 2 | Dst | TVec | source TVec |
| 3 | FrameSize | Int32 |
Result: stored in self (calling object)
A simple and fast envelope detector using moving average filter. FrameSize defines the length of the moving average (low pass filter) and the downsampling factor. The result is placed in Dst. Dst.Length = Src.Length div FrameSize. Src.Length must be divisable by FrameSize. This routine is 10 to 100x times faster then a decimator based envelope detector. Its drawback is higher noise due to some aliasing. Envelope detection is used to find the frequency of events with "long" periods.
For example:
sampling frequency is 11kHz. The audio card is recording hammer impacts which occur once every five seconds. Because the duration of the hammer impact is very short, the 0.2 Hz frequency will not show up in the frequency spectrum of the signal, regardless of the frequency resolution, especially because the audio card filters out everything below 20 Hz. By selecting FrameSize = 2000 the sampling frequency will be reduced by 2000x, by averaging together groups of rectified samples. The frequency spectrum of the filtered signal will show a clear peak at 0.2 Hz.
using Dew.Math;
using Dew.Math.Editors;
using Dew.Math.Units;
using Dew.Signal;
using Dew.Signal.Units;
using Dew.Math.Tee;
using Dew.Signal.Tee;
private void button1_Click(object sender, EventArgs e)
{
Vector Response = new Vector(0);
Vector x;
double FS = 2;
Vector b = MtxExpr.Sin(MtxExpr.Ramp(3000, TMtxFloatPrecision.mvDouble, 0,2*Math.PI*0.02/FS))*
MtxExpr.Sin(MtxExpr.Ramp(3000, TMtxFloatPrecision.mvDouble, 0,2*Math.PI*0.2/FS));
Vector c = new Vector(b.Length);
SignalUtils.EnvelopeDetector(b,c,10); //reduce sampling frequency by 10x
SignalUtils.FrequencyResponse(b, null, Response, 8, true, TSignalWindowType.wtHanning, 0);
x = MtxExpr.Ramp(Response.Length, TMtxFloatPrecision.mvDouble, 0,1.0/Response.Length);
MtxVecTee.DrawIt(x, Response, "Original frequency spectrum", false);
SignalUtils.FrequencyResponse(c,null,Response,8,true, TSignalWindowType.wtHanning, 0);
x = MtxExpr.Ramp(Response.Length, TMtxFloatPrecision.mvDouble, 0,1.0/Response.Length);
MtxVecTee.DrawIt(x, Response, "Envelope frequency spectrum", false);
}