Exemple #1
0
    public double[] synt_noise(Bitmap image, int samplerate)
    {
        int samples = (int)image.Width * samplerate / pixpersec;

        Complex[] noise = GlobalMembersSpectrogram.get_pink_noise(samplerate * 10);        // 10 sec loop

        double     filterscale = ((double)noise.Length * 2) / samplerate;
        Filterbank filterbank  = Filterbank.get_filterbank(frequency_axis, filterscale, basefreq, bandwidth, overlap);

        int top_index = (int)(maxfreq * filterscale);

        double[] @out = new double[samples];

        for (int bandidx = 0; bandidx < image.Height; ++bandidx)
        {
            //if (cancelled())
            //	return List<int>();
            band_progress(bandidx, image.Height - 1);

            // filter noise
            Pair <int, int> range = filterbank.get_band(bandidx);
            //std::cout << bandidx << "/"<<image.height()<<"\n";
            Console.Out.WriteLine("(noise) sample: {0}", range.Second - range.First);

            double[] filtered_noise = new double[noise.Length];
            //std.copy(noise.begin()+range.first, noise.begin()+Math.Min(range.second, top_index), filtered_noise.begin()+range.first);

            // ifft noise
            double[] noise_mod = GlobalMembersSpectrogram.padded_IFFT(filtered_noise);

            // resample spectrogram band
            double[] envelope = GlobalMembersSpectrogram.resample(envelope_from_spectrogram(image, bandidx), samples);

            // modulate with looped noise
            for (uint i = 0; i < samples; ++i)
            {
                @out[i] += envelope[i] * noise_mod[i % noise_mod.Length];
            }
        }

        GlobalMembersSpectrogram.normalize_signal(ref @out);
        return(@out);
    }
Exemple #2
0
    public Bitmap to_image(ref double[] signal, int samplerate)
    {
        Console.Out.WriteLine("Transforming input");

        Complex[] spectrum = GlobalMembersSpectrogram.padded_FFT(signal);

        //const size_t width = (spectrum.size()-1)*2*pixpersec/samplerate;
        double w1    = spectrum.Length - 1;
        double w2    = (double)pixpersec / (double)samplerate;
        double w3    = w1 * 2 * w2;
        int    width = (int)w3;

        // transformation of frequency in hz to index in spectrum
        //const double filterscale = ((double)spectrum.size()*2)/samplerate;
        double filterscale = ((double)spectrum.Length * 2) / samplerate;

        Console.Out.WriteLine("filterscale: {0}", filterscale);

        Filterbank filterbank = Filterbank.get_filterbank(frequency_axis, filterscale, basefreq, bandwidth, overlap);
        int        bands      = (int)filterbank.num_bands_est(maxfreq);
        int        top_index  = (int)(maxfreq * filterscale);

        // maxfreq has to be at most nyquist
        Debug.Assert(top_index <= spectrum.Length);

        //std::vector<real_vec> image_data;
        List <double[]> image_data = new List <double[]>();

        for (int bandidx = 0;; ++bandidx)
        {
            band_progress(bandidx, bands);

            // filtering
            Pair <int, int> range = filterbank.get_band(bandidx);

            Console.Out.WriteLine("-----");
            Console.Out.WriteLine("spectrum size: {0}", spectrum.Length);
            Console.Out.WriteLine("lowidx: {0:0.00} highidx: {1:0.00}", range.First, range.Second);
            Console.Out.WriteLine("(real)lowfreq: {0:0.00} (real)highfreq: {1:0.00}", range.First / filterscale, range.Second / filterscale);
            Console.Out.WriteLine("actual width: {0:0.00} hz", (range.Second - range.First) / filterscale);
            Console.Out.WriteLine("vertical values: {0:0.00}", (range.Second - range.First));
            Console.Out.WriteLine("crowd sample: {0:0.00}", (range.Second - range.First - 1) * 2);
            Console.Out.WriteLine("theoretically staci: {0:0.00} hz samplerate", 2 * (range.Second - range.First) / filterscale);
            Console.Out.WriteLine("width: {0}", width);

            int       filterbandLength = range.Second - range.First;
            Complex[] filterband       = new Complex[filterbandLength];
            //std::copy(spectrum.begin()+range.first,
            //          spectrum.begin()+std::min(range.second, top_index),
            //          filterband.begin());
            int sourceIndexStart = range.First;
            int sourceIndexEnd   = Math.Min(range.Second, top_index);
            int length           = sourceIndexEnd - sourceIndexStart;
            Array.Copy(spectrum, sourceIndexStart, filterband, 0, length);

            if (range.First > top_index)
            {
                break;
            }
            if (range.Second > top_index)
            {
                int start = top_index - range.First;
                //std::fill(filterband.begin()+top_index-range.first,
                //          filterband.end(), Complex(0,0));
                break;                 // TODO: fix above
            }

            // windowing
            apply_window(ref filterband, range.First, filterscale);

            // envelope detection + resampling
            double[] envelope = GlobalMembersSpectrogram.resample(GlobalMembersSpectrogram.get_envelope(ref filterband), width);

            image_data.Add(envelope);
        }

        GlobalMembersSpectrogram.normalize_image_cutoff_negative(ref image_data);

        return(make_image(image_data));
    }