private static int ResolveRequiredChunkLength(WaveformDescription waveform) { if (waveform == null) { throw new ArgumentNullException("waveform"); } if (waveform.Waveform == Waveform.Dc) { return(1); } return((int)waveform.SamplingRate); }
/// <devdoc> /// If chunck length is higher than sampling rate then then only first one second epoch /// will contain the impulse. /// </devdoc> private static double[] CreateImpulse(WaveformDescription waveform, int chunckLength) { Debug.Assert(waveform != null); Debug.Assert(waveform.SamplingRate > 0); double[] samples = new double[(int)waveform.SamplingRate]; int impulseIndex = (int)((waveform.Phase / (Math.PI * 2)) * waveform.SamplingRate); for (int i = 0; i < samples.Length; ++i) { samples[i] = waveform.Offset + (i == impulseIndex ? waveform.Amplitude : 0); } return(samples); }
private static double[] CreateDc(WaveformDescription waveform, int chunckLength) { Debug.Assert(waveform != null); Debug.Assert(chunckLength > 0); double[] samples = new double[chunckLength]; double value = waveform.Amplitude + waveform.Offset; for (int i = 0; i < samples.Length; ++i) { samples[i] = value; } return(samples); }
private static double[] CreateSine(WaveformDescription waveform, int chunckLength) { Debug.Assert(waveform != null); Debug.Assert(waveform.SamplingRate > 0); Debug.Assert((int)waveform.SamplingRate == chunckLength, "Generation of epochs longer than one second is not currently supported."); if (waveform.Frequency == 0) { return(CreateDc(waveform, chunckLength)); } double[] samples = new double[chunckLength]; double n = (waveform.Frequency / waveform.SamplingRate) * (Math.PI * 2); for (int i = 0; i < samples.Length; ++i) { samples[i] = waveform.Offset + waveform.Amplitude * Math.Sin(waveform.Phase + i * n); } return(samples); }
public static double[] Create(WaveformDescription waveform, int chunckLength) { Debug.Assert(waveform != null); if (waveform.Waveform == Waveform.Dc) { return(CreateDc(waveform, chunckLength)); } if (waveform.Waveform == Waveform.Sine) { return(CreateSine(waveform, chunckLength)); } if (waveform.Waveform == Waveform.Impulse) { return(CreateImpulse(waveform, chunckLength)); } throw new InvalidEnumArgumentException("waveform.Waveform", (int)waveform.Waveform, typeof(Waveform)); }
/// <summary> /// Initializes a new instance of <see cref="WaveGenerator"/>. /// </summary> /// <param name="waveform"> /// Description of the waveform to synthesize. Note that, for <see cref="Waveform.Sine"/>, if sampling rate <see cref="WaveformDescription.SamplingRate"/> /// is not an integer value then there may be a visible error at the end of each second in the generated waveform. /// Also note that, because waveform is synthesized when this object is constructed, there may be an <see cref="OutOfMemoryException"/> /// if sampling rate is extremely high (one full epoch of one second will be generated for all waveforms but <see cref="Waveform.Dc"/>) /// or <see cref="ArgumentOutOfRangeException"/> when you set <c>WaveformDescription.SamplingRate</c> if required value is higher /// than <see cref="WaveformDescription.MaximumSamplingRate"/>. /// </param> /// <exception cref="ArgumentNullException"> /// If <paramref name="waveform"/> is <see langword="null"/>. /// </exception> public WaveGenerator(WaveformDescription waveform) : base(WaveformFactory.Create(waveform, ResolveRequiredChunkLength(waveform))) { }