예제 #1
0
 public InverseRealFft(int fftSize)
 {
     FftSize    = fftSize;
     Waveform   = new AlignedArrayDouble(16, fftSize);
     Spectrum   = new AlignedArrayComplex(16, fftSize);
     InverseFft = FftwPlanRC.Create(Waveform, Spectrum, DftDirection.Backwards, PlannerFlags.Estimate);
 }
예제 #2
0
        public AudioIn(int sampleRate, int device, int bufferSize)
        {
            if (device > WaveIn.DeviceCount)
            {
                throw new Exception();
            }

            this.sampleRate = sampleRate;
            this.bufferSize = bufferSize;
            audioDataTrue   = new short[bufferSize + 20286];
            tempOdd         = new double[audioDataTrue.Length];
            tempTrue        = new double[audioDataTrue.Length];

            realIn = new PinnedArray <double>(audioDataTrue.Length);
            comOut = new FftwArrayComplex(DFT.GetComplexBufferSize(realIn.GetSize()));
            fft    = FftwPlanRC.Create(realIn, comOut, DftDirection.Forwards);

            waveInDevices            = WaveIn.DeviceCount;
            waveEvent                = new WaveInEvent();
            waveEvent.DeviceNumber   = device;
            waveEvent.DataAvailable += OnWaveIn;
            channels             = WaveIn.GetCapabilities(device).Channels;
            waveEvent.WaveFormat = new WaveFormat(sampleRate, 1);

            string path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "/NetworkSave01.json";

            // Load Neural network
            if (File.Exists(path))
            {
                Console.WriteLine("exists");
                network = NeuralNetwork.LoadNetwork(path);
            }
            else
            {
                throw new FileNotFoundException();
            }

            //network.PrintWeights();

            waveEvent.StartRecording();
        }
예제 #3
0
        static void Main(string[] args)
        {
            try
            {
                TcpClient tcpClient = new TcpClient("127.0.0.1", 10000);
                Console.WriteLine("Connected");

                StreamReader reader = new StreamReader(tcpClient.GetStream());
                StreamWriter writer = new StreamWriter(tcpClient.GetStream());
                string       s      = "";

                using (var timeDomain = new PinnedArray <double>(inputSize))
                    using (var frequencyDomain = new FftwArrayComplex(DFT.GetComplexBufferSize(timeDomain.GetSize())))
                        using (var fft = FftwPlanRC.Create(timeDomain, frequencyDomain, DftDirection.Forwards))
                        {
                            while (!(s = reader.ReadLine()).Equals("Quit") || (s == null))
                            {
                                string result = "";

                                if (IsValidJson(s))
                                {
                                    result = ProcessMessage(s, timeDomain, frequencyDomain, fft);
                                }

                                writer.WriteLine(result);
                                writer.Flush();
                                GC.Collect();
                            }
                            reader.Close();
                            writer.Close();
                            tcpClient.Close();
                        }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Console.WriteLine(e.InnerException);
                Console.WriteLine(e.Message);
                throw;
            }
        }
예제 #4
0
        private static string ProcessMessage(string s, PinnedArray <double> pin, FftwArrayComplex com, FftwPlanRC fft)
        {
            //Deserialize and then FFTW then return datasample as json string
            WFOTransporter wav    = JsonConvert.DeserializeObject <WFOTransporter>(s);
            WaveFileObject obj    = new WaveFileObject(wav);
            DataSample     sample = new DataSample(FFT(obj, pin, com, fft));
            string         json   = JsonConvert.SerializeObject(sample);

            return(json);
        }
예제 #5
0
        static double[] FFT(WaveFileObject obj, PinnedArray <double> pin, FftwArrayComplex com, FftwPlanRC fft)
        {
            Console.WriteLine("FFT");
            double[] magnitudes;
            Console.WriteLine(obj.soundData.Count);
            double[] input = new double[obj.soundData.Count + 20286];
            Array.Clear(input, 0, input.Length);
            obj.soundData.CopyTo(input, 0);

            switch (obj.header.channels)
            {
            case 1:
                for (int i = 0; i < pin.Length; i++)
                {
                    pin[i] = input[i];
                    //Console.Write(pin[i] + " : ");
                }
                break;

            case 2:
                for (int i = 0; i < pin.Length; i++)
                {
                    pin[i] = input[i + i];
                    //Console.WriteLine(pin[i]);
                }
                break;

            default:
                break;
            }

            fft.Execute();

            magnitudes = new double[com.Length];
            for (int i = 0; i < 4000; i++)
            {
                magnitudes[i] = 10 * Math.Log10((com[i].Magnitude / inputSize) * (com[i].Magnitude / inputSize));

                /*
                 * if (10 * Math.Log10((com[i].Magnitude / inputSize) * (com[i].Magnitude / inputSize)) > 10)
                 * {
                 *  Console.WriteLine("Bin: " + i * sampleRate / com.Length + " " + 10 * Math.Log10((com[i].Magnitude / inputSize) * (com[i].Magnitude / inputSize)));
                 * }
                 */
            }

            Console.WriteLine(com.Length);
            Console.WriteLine();

            Console.WriteLine("Returning magnitudes");
            return(magnitudes);
        }