Mat CreateBuffer(int bufferLength, long sampleOffset, double frequency, double phase) { var buffer = new double[bufferLength]; if (frequency > 0) { var period = 1.0 / frequency; var waveform = Waveform; switch (waveform) { default: case FunctionWaveform.Sine: frequency = frequency * TwoPI; for (int i = 0; i < buffer.Length; i++) { buffer[i] = Math.Sin(frequency * (i + sampleOffset) + phase); } break; case FunctionWaveform.Triangular: phase = NormalizedPhase(phase) / TwoPI; for (int i = 0; i < buffer.Length; i++) { var t = frequency * (i + sampleOffset + period / 4) + phase; buffer[i] = (1 - (4 * Math.Abs((t % 1) - 0.5) - 1)) - 1; } break; case FunctionWaveform.Square: case FunctionWaveform.Sawtooth: phase = NormalizedPhase(phase) / TwoPI; for (int i = 0; i < buffer.Length; i++) { var t = frequency * (i + sampleOffset + period / 2) + phase; buffer[i] = 2 * (t % 1) - 1; if (waveform == FunctionWaveform.Square) { buffer[i] = Math.Sign(buffer[i]); } } break; } } var result = new Mat(1, buffer.Length, Depth.GetValueOrDefault(OpenCV.Net.Depth.F64), 1); using (var bufferHeader = Mat.CreateMatHeader(buffer)) { CV.ConvertScale(bufferHeader, result, Amplitude, Offset); return(result); } }