/** * Creates a basic {@link Network} with 1 {@link Region} and 1 {@link PALayer}. However * this basic network contains all algorithmic components. * * @return a basic Network */ internal Network.Network CreateBasicNetwork() { Parameters p = NetworkDemoHarness.GetParameters(); p = p.Union(NetworkDemoHarness.GetNetworkDemoTestEncoderParams()); // This is how easy it is to create a full running Network! var sineData = SineGenerator.GenerateSineWave(100, 100 * 20, 10, 1) .Select(s => s.ToString(NumberFormatInfo.InvariantInfo)); string[] header = new[] { "sinedata", "float", "" }; object[] n = { "sine", header.Union(sineData).ToObservable() }; SensorParams parms = SensorParams.Create(SensorParams.Keys.Obs, n); Sensor <ObservableSensor <string[]> > sensor = Sensor <ObservableSensor <string[]> > .Create(ObservableSensor <string[]> .Create, parms); return(Network.Network.Create("Network API Demo", p) .Add(Network.Network.CreateRegion("Region 1") .Add(Network.Network.CreateLayer("Layer 2/3", p) .AlterParameter(Parameters.KEY.AUTO_CLASSIFY, true) .Add(Anomaly.Create()) .Add(new TemporalMemory()) .Add(new Algorithms.SpatialPooler()) .Add(sensor)))); }
public override List <SignalSample> GetSamples() { SineGenerator sine = new SineGenerator(0.2d, 2d, 1d, 90d); SignalSampler sampler = new SignalSampler(sine); return(sampler.GetSamples(20d, 0.1d)); }
public void TestSampling() { foreach (double sample in SineGenerator.GenerateSineWave(100, 100, 10, 1)) { Assert.AreEqual(0, sample, 10.0); } }
public void CanCreateWaveOutDevice() { WaveOut waveOut = new WaveOut(); var source = new SineGenerator().ToWaveSource(16); waveOut.Initialize(source); waveOut.Dispose(); }
public void ApplyTest() { int n = 16384; int sampleRate = 1000; double f1 = 22; double f2 = 300; Signal cosine = new CosineGenerator(f1, 1, sampleRate).Generate(n); Signal sine = new SineGenerator(f2, 1, sampleRate).Generate(n); var merge = new AddFilter(cosine); merge.Normalize = true; Signal original = merge.Apply(sine); var of1 = FindFrequencyCount(sampleRate, original, f1); var of2 = FindFrequencyCount(sampleRate, original, f2); Assert.AreEqual(2.1919473889115457E-05, of1, 1e-8); Assert.AreEqual(2.9269612275882952E-05, of2, 1e-8); Signal lowFiltered1 = new LowPassFilter(f1, sampleRate).Apply(original); Signal lowFiltered2 = new LowPassFilter(f2, sampleRate).Apply(original); Signal highFiltered1 = new HighPassFilter(f1, sampleRate).Apply(original); Signal highFiltered2 = new HighPassFilter(f2, sampleRate).Apply(original); var lf11 = FindFrequencyCount(sampleRate, lowFiltered1, f1); var lf12 = FindFrequencyCount(sampleRate, lowFiltered1, f2); Assert.AreEqual(1.5008301894378632E-05, lf11, 1e-8); // should be higher Assert.AreEqual(2.33561994410787E-06, lf12, 1e-8); Assert.IsTrue(lf11 > lf12); var lf21 = FindFrequencyCount(sampleRate, lowFiltered2, f1); var lf22 = FindFrequencyCount(sampleRate, lowFiltered2, f2); Assert.AreEqual(2.1754311480113727E-05, lf21, 1e-8); // should not have much difference Assert.AreEqual(1.6551627449395776E-05, lf22, 1e-8); var hf11 = FindFrequencyCount(sampleRate, highFiltered1, f1); var hf12 = FindFrequencyCount(sampleRate, highFiltered1, f2); Assert.AreEqual(1.4979563644182712E-05, hf11, 1e-8); // should not have much difference Assert.AreEqual(2.7342436340623498E-05, hf12, 1e-8); var hf21 = FindFrequencyCount(sampleRate, highFiltered2, f1); var hf22 = FindFrequencyCount(sampleRate, highFiltered2, f2); Assert.AreEqual(1.5938293048394034E-06, hf21, 1e-8); Assert.AreEqual(1.420896515288728E-05, hf22, 1e-8); // should be higher Assert.IsTrue(hf22 > hf21); Assert.AreEqual(16384, cosine.Duration.TotalMilliseconds); Assert.AreEqual(16384, sine.Duration.TotalMilliseconds); Assert.AreEqual(16384, original.Duration.TotalMilliseconds); }
public void ApplyTest() { int n = 16384; int sampleRate = 1000; double f1 = 22; double f2 = 300; Signal cosine = new CosineGenerator(f1, 1, sampleRate).Generate(n); Signal sine = new SineGenerator(f2, 1, sampleRate).Generate(n); var merge = new AddFilter(cosine); merge.Normalize = true; Signal original = merge.Apply(sine); var of1 = FindFrequencyCount(sampleRate, original, f1); var of2 = FindFrequencyCount(sampleRate, original, f2); Assert.AreEqual(0.359128660199268, of1, 1e-8); Assert.AreEqual(0.47955332752802149, of2, 1e-8); Signal lowFiltered1 = new LowPassFilter(f1, sampleRate).Apply(original); Signal lowFiltered2 = new LowPassFilter(f2, sampleRate).Apply(original); Signal highFiltered1 = new HighPassFilter(f1, sampleRate).Apply(original); Signal highFiltered2 = new HighPassFilter(f2, sampleRate).Apply(original); var lf11 = FindFrequencyCount(sampleRate, lowFiltered1, f1); var lf12 = FindFrequencyCount(sampleRate, lowFiltered1, f2); Assert.AreEqual(0.24589601823749971, lf11, 1e-8); // should be higher Assert.AreEqual(0.038266797164259778, lf12, 1e-8); Assert.IsTrue(lf11 > lf12); var lf21 = FindFrequencyCount(sampleRate, lowFiltered2, f1); var lf22 = FindFrequencyCount(sampleRate, lowFiltered2, f2); Assert.AreEqual(0.35642263929018364, lf21, 1e-8); // should not have much difference Assert.AreEqual(0.271181864130875, lf22, 1e-8); var hf11 = FindFrequencyCount(sampleRate, highFiltered1, f1); var hf12 = FindFrequencyCount(sampleRate, highFiltered1, f2); Assert.AreEqual(0.24542517074628975, hf11, 1e-8); // should not have much difference Assert.AreEqual(0.44797847700473359, hf12, 1e-8); var hf21 = FindFrequencyCount(sampleRate, highFiltered2, f1); var hf22 = FindFrequencyCount(sampleRate, highFiltered2, f2); Assert.AreEqual(0.026113299330488803, hf21, 1e-8); Assert.AreEqual(0.23279968506488344, hf22, 1e-8); // should be higher Assert.IsTrue(hf22 > hf21); Assert.AreEqual(16384, cosine.Duration.TotalMilliseconds); Assert.AreEqual(16384, sine.Duration.TotalMilliseconds); Assert.AreEqual(16384, original.Duration.TotalMilliseconds); }
static void TestFFMPEG() { var videoWriter = new VideoFileWriter(); int width = 800; int height = 600; int framerate = 24; string path = Path.GetFullPath("output.webm"); int videoBitRate = 1200 * 1000; int audioFrameSize = 44100; int audioBitRate = 128000; int audioSampleRate = 44100; AudioLayout audioChannels = AudioLayout.Mono; videoWriter.Width = width; videoWriter.Height = height; videoWriter.FrameRate = framerate; videoWriter.VideoCodec = VideoCodec.Vp8; videoWriter.BitRate = videoBitRate; videoWriter.PixelFormat = AVPixelFormat.FormatYuv420P; videoWriter.Open(path); //, audioFrameSize, audioChannels, audioSampleRate, AudioCodec.Vorbis, audioBitRate); var a = new Accord.DirectSound.AudioDeviceCollection(DirectSound.AudioDeviceCategory.Capture); // Generate 1 second of audio SineGenerator gen = new SineGenerator() { SamplingRate = audioSampleRate, Channels = 1, Format = SampleFormat.Format16Bit, Frequency = 10, Amplitude = 1000.9f, }; Signal s = gen.Generate(TimeSpan.FromSeconds(255)); //s.Save("test.wav"); var m2i = new MatrixToImage(); Bitmap frame; for (byte i = 0; i < 255; i++) { byte[,] matrix = Matrix.Create(height, width, i); m2i.Convert(matrix, out frame); videoWriter.WriteVideoFrame(frame, TimeSpan.FromSeconds(1)); //// Generate 1 second of audio //s = gen.Generate(TimeSpan.FromSeconds(1)); //videoWriter.WriteAudioFrame(s); } videoWriter.Close(); }
public void GetSignal_ChangeTime_ReturnsSignalLevel( double amplitude, double frequency, double phase, double time, double expectedSignalLevel) { var sineGenerator = new SineGenerator(amplitude, frequency, phase); var result = sineGenerator.GetSignal(time); result = Math.Round(result, 10); Assert.AreEqual(expectedSignalLevel, result); }
public IWaveSource CreateASineSource() { double frequency = 1200; double amplitude = 0.6; double phase = 0.0; //Create a ISampleSource SG = new SineGenerator(frequency, amplitude, phase); //Convert the ISampleSource into a IWaveSource return(SG.ToWaveSource()); }
public void DmoResamplerTest() { var source = new SineGenerator().ToWaveSource(16); using (DmoResampler resampler = new DmoResampler(source, 11500)) { byte[] buffer = new byte[source.WaveFormat.BytesPerSecond / 2]; if (resampler.Read(buffer, 0, buffer.Length) != buffer.Length) { throw new Exception("Could not fill the whole buffer"); } } }
public void TestGeneratesASineInEmptyBuffer() { ISampleProvider generator = new SineGenerator( new OutputFormat(new SampleRate(44100), 1), new ConstantSampleSource(440.0f), new ConstantSampleSource(1.0f) ); var buffer = new float[22050]; var acb = AudioChannelBuffer.CreateFromRawBuffer(buffer, 0, buffer.Length); generator.Read(acb); var actual = ToReadableString(buffer); var expected = File.ReadAllText(GetTestFileName()); Assert.Equal(expected, actual); }
public void startPlaying() { foreach (double freq in frequencies) { SineGenerator generator = new SineGenerator(freq, 1.0, 0); VolumeSource vol; mixer.AddSource( generator.ToWaveSource() .AppendSource(x => new DmoChannelResampler(x, monoToStereoChannelMatrix, sampleRate)) .AppendSource(x => new VolumeSource(x.ToSampleSource()), out vol) ); } soundOut.Initialize(mixer.ToWaveSource()); soundOut.Play(); }
public static float[] CreateTable(int size, WaveformEnum type) { Generator generator; if (type == WaveformEnum.Sine) { generator = new SineGenerator(new GeneratorDescriptor()); } else if (type == WaveformEnum.Square) { generator = new SquareGenerator(new GeneratorDescriptor()); } else if (type == WaveformEnum.Triangle) { generator = new TriangleGenerator(new GeneratorDescriptor()); } else if (type == WaveformEnum.Saw) { generator = new SawGenerator(new GeneratorDescriptor()); } else if (type == WaveformEnum.WhiteNoise) { generator = new WhiteNoiseGenerator(new GeneratorDescriptor()); } else { return(null); } float[] table = new float[size]; double phase, increment; phase = generator.StartPhase; increment = generator.Period / size; for (int x = 0; x < table.Length; x++) { table[x] = generator.GetValue(phase); phase += increment; } return(table); }
public static void Main(string[] args) { Console.WriteLine(MidiIn.DeviceInfo(0).ProductName); var midiIn = new MidiIn(0); var output = new WaveOut(); midiIn.Start(); var frequencySampleSource = new MidiDataSampleSource(midiIn); var sampleRate = new SampleRate(44100); var outputFormat = new OutputFormat(sampleRate, 1); var amplitudeSampleSource = new ConstantSampleSource(0.5f); var sampleSource = new SineGenerator(outputFormat, frequencySampleSource, amplitudeSampleSource); var nAudioSampleProvider = new NAudioSampleProvider(sampleSource, outputFormat); output.Init(nAudioSampleProvider); output.Play(); while (true) { Thread.Sleep(100); } }
public void write_video_new_api() { string basePath = TestContext.CurrentContext.TestDirectory; #region doc_new_api // Let's say we would like to save file using a .mp4 media // container, a H.265 video codec for the video stream, and // AAC for the audio stream, into the file: string outputPath = Path.Combine(basePath, "output_audio.avi"); // First, we create a new VideoFileWriter: var videoWriter = new VideoFileWriter() { // Our video will have the following characteristics: Width = 800, Height = 600, FrameRate = 24, BitRate = 1200 * 1000, VideoCodec = VideoCodec.H265, AudioCodec = AudioCodec.Aac, AudioBitRate = 44100, AudioLayout = AudioLayout.Stereo, FrameSize = 44100, PixelFormat = AVPixelFormat.FormatYuv420P }; // We can open for it writing: videoWriter.Open(outputPath); // At this point, we can check the console of our application for useful // information regarding the media streams created by FFMPEG. We can also // check those properties using the class itself, specially for properties // that we didn't set beforehand but that have been filled by FFMPEG: int width = videoWriter.Width; int height = videoWriter.Height; int frameRate = videoWriter.FrameRate.Numerator; int bitRate = videoWriter.BitRate; VideoCodec videoCodec = videoWriter.VideoCodec; AudioCodec audioCodec = videoWriter.AudioCodec; AudioLayout audioLayout = videoWriter.AudioLayout; int audioChannels = videoWriter.NumberOfChannels; // We haven't set those properties, but FFMPEG has filled them for us: int audioSampleRate = videoWriter.SampleRate; int audioSampleSize = videoWriter.FrameSize; // Now, let's say we would like to save dummy images of // changing color, with a sine wave as the audio stream: var g = new SineGenerator() { Channels = 1, // we will generate only one channel, and the file writer will convert on-the-fly Format = SampleFormat.Format32BitIeeeFloat, Frequency = 10f, Amplitude = 0.9f, SamplingRate = 44100 }; var m2i = new MatrixToImage(); Bitmap frame; for (byte i = 0; i < 255; i++) { // Create bitmap matrix from a matrix of RGB values: byte[,] matrix = Matrix.Create(height, width, i); m2i.Convert(matrix, out frame); // Write the frame to the stream. We can optionally specify // the moment when this frame should remain in the stream: videoWriter.WriteVideoFrame(frame, TimeSpan.FromSeconds(i)); // We can also write the audio samples if we need to: Signal signal = g.Generate(TimeSpan.FromSeconds(1)); // generate 1 second of audio videoWriter.WriteAudioFrame(signal); // save it to the stream } // We can get how long our written video is: TimeSpan duration = videoWriter.Duration; // Close the stream videoWriter.Close(); #endregion Assert.AreEqual(2540000000, duration.Ticks); Assert.AreEqual(800, width); Assert.AreEqual(600, height); Assert.AreEqual(24, frameRate); Assert.AreEqual(1200000, bitRate); Assert.AreEqual(VideoCodec.H265, videoCodec); Assert.AreEqual(AudioCodec.Aac, audioCodec); Assert.AreEqual(44100, audioSampleRate); Assert.AreEqual(AudioLayout.Stereo, audioLayout); Assert.AreEqual(2, audioChannels); }