Exemplo n.º 1
0
 private async void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
 {
     ImageSensor.ShutDown();
     if (ViewModel.Machine.Connected)
     {
         await ViewModel.Machine.DisconnectAsync();
     }
 }
Exemplo n.º 2
0
 private void MainForm_Resize(object sender, EventArgs e)
 {
     AreaWidth     = Width;
     AreaHeight    = Height;
     camera.Aspect = (double)Width / Height;
     imageSensor   = new ImageSensor(Width, Height);
     canvas        = new Bitmap(AreaWidth, AreaHeight);
     canvasLock    = new LockBitmap(canvas);
 }
Exemplo n.º 3
0
        static double SimulateCamera(ImageSensor sensor, double input)
        {
            Matrix <double> sensor_input = Matrix <double> .Build.Dense(sensor.Rows, sensor.Columns);

            double[] sensor_input_arr = sensor_input.AsColumnMajorArray();
            for (int k = 0; k < sensor_input_arr.Length; k++)
            {
                sensor_input_arr[k] = input;
            }
            Matrix <double> sensor_result = sensor.Simulate(sensor_input);

            return(sensor_result[0, 0]);
        }
Exemplo n.º 4
0
        private void MainForm_Shown(object sender, EventArgs e)
        {
            if (!hasBeenInitialized)
            {
                AreaWidth  = Width;
                AreaHeight = Height;

                imageSensor = new ImageSensor(Width, Height);

                hasBeenInitialized = true;

                canvas = new Bitmap(AreaWidth, AreaHeight);

                Init();
            }
        }
Exemplo n.º 5
0
        public static Matrix <double> GenerateTestPattern(ImageSensor sensor, double incident_ph_flux)
        {
            Matrix <double> test_target = Matrix <double> .Build.Dense(sensor.Rows, sensor.Columns);

            int  fringe_width = 1;
            int  col_start = 1, col_end = col_start + fringe_width;
            bool end = false;

            while (!end)
            {
                for (int row = 0; row < test_target.RowCount / 2; row++)
                {
                    for (int col = col_start; col < test_target.ColumnCount && col < col_end; col++)
                    {
                        test_target[row, col] = 1.0;
                    }
                }
                for (int row = test_target.RowCount / 2; row < test_target.RowCount; row++)
                {
                    for (int col = col_start; col < test_target.ColumnCount && col < col_end; col++)
                    {
                        test_target[row, col] = ((double)col) / test_target.ColumnCount;
                    }
                }
                fringe_width++;
                col_start += 2 * fringe_width;
                col_end    = col_start + fringe_width;

                if (col_end >= test_target.ColumnCount)
                {
                    end = true;
                }
            }

            test_target *= incident_ph_flux;

            test_target = test_target.Transpose();

            return(sensor.Simulate(test_target));
        }
Exemplo n.º 6
0
        public static ImageSensor CustomSensor(
            int rows, int cols, double temperature = 323.15, double pga_gain = 1.0, double exposure_time = 0.035,
            bool activate_noise = true)
        {
            ImageSensorProperties properties = new ImageSensorProperties();

            properties.ImageSensorRows    = rows;
            properties.ImageSensorColumns = cols;

            properties.PDET_PitchX = 5.0e-6;
            properties.PDET_PitchY = 5.0e-6;

            properties.SIMFL_PhotonShotNoise         = activate_noise;
            properties.SIMFL_PhotoelectronsShotNoise = activate_noise;

            properties.SensorKind = SensorKind.CCD;

            properties.Temperature = temperature;

            properties.PDET_FillFactor = 0.95;

            properties.IntegrationTime    = exposure_time;
            properties.PDET_ExtQuantumEff = 0.8;
            properties.PDET_QuantumYield  = 1.0;

            properties.SIMFL_PhotoResponseNonUniformity = activate_noise;
            properties.PDET_Light_PRNU_Model            = NoiseDistribution.JanesickGaussian;
            properties.PDET_Light_PRNU_Sigma            = 0.01;

            properties.M_BandGap_0    = 1.166;
            properties.M_VarshniAlpha = 5.5e-04;
            properties.M_VarshniBeta  = 636.0;

            properties.SIMFL_DarkCurrent          = activate_noise;
            properties.PDET_DarkCurrentFigOfMerit = 1.0 * 1.0e-9 * 1.0e4;
            properties.PDET_DarkCurrentCoef_ca    = 4.31e5;
            properties.PDET_DarkCurrentCoef_ed    = 2.0;

            properties.SIMFL_DarkCurrentShotNoise = activate_noise;
            // There was a Gaussian noise model in the original code but it was not used.

            properties.SIMFL_DarkCurrentFPN = activate_noise;
            properties.PDET_Dark_PRNU_Model = NoiseDistribution.LogNormal;
            properties.PDET_Dark_FPN_Sigma  = 0.4;

            properties.SN_Gain        = 5.0e-6;
            properties.SN_V_ref_Reset = 3.1;
            properties.SN_Vs_min      = 0.5;
            properties.SIMFL_SenseNodeNonlinearGainResponse = false;// activate_noise;
            properties.SN_GainResponse_k1   = 1.090900000e-14;
            properties.SN_FullEellElectrons = 2.0e4;

            properties.SIMFL_SenseNodeResetNoise = activate_noise;
            properties.SN_ResetNoiseFactor       = 0.8;
            // There was a Gaussian reset noise model but it was not used anywhere.

            properties.SF_Gain = 1.0;

            properties.SIMFL_SourceFollowerNonLinearity = activate_noise;
            properties.SF_Nonlinearity_Ratio            = 1.05;
            properties.SF_Flicker_Corner_Freq           = 1.0e6;
            properties.SF_White_Noise_Density           = 15.0e-9;
            properties.SF_Delta_Induced_Modulation      = 1.0e-8;
            properties.SF_Data_Clock_Speed       = 20.0e6;
            properties.SF_Freq_Sampling_Delta    = 10000.0;
            properties.SIMFL_SourceFollowerNoise = activate_noise;

            properties.SIMFL_SourceFollowerFixedPatternOffset = activate_noise;
            properties.SF_Fixed_Pattern_Offset_Model          = NoiseDistribution.JanesickGaussian;
            properties.SF_Fixed_Pattern_Offset_Sigma          = 0.0005;

            properties.SF_CDS_Sample_to_Sampling_Time = 1.0e-6;
            properties.SF_CDS_Gain = 1.0;

            properties.ADC_Resolution         = 8;
            properties.ADC_Offset             = 0.0;
            properties.SIMFL_ADC_Nonlinearity = false; // was always false
            properties.ADC_NonlinearityRatio  = 1.1;

            properties.PGA_Gain = pga_gain;

            ImageSensor sensor = new ImageSensor(properties);

            return(sensor);
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            //int slm_size = 256;
            //int t_vecs_count = 1024;
            int slm_size     = 64;  // Set the number of the SLM channels
            int t_vecs_count = 512; // Set the number of transmission vectors for the filter calculation
            // SLM: 64, TVEC: 512
            // SLM: 128, TVEC: 1024
            // SLM: 256, TVEC: 2048
            // SLM: 512, TVEC: 4096
            Complex e_field    = new Complex(1.0, 0.0); // Incident electric field
            double  wavelength = 632.8e-9;
            //Complex e_inc = new Complex(1.0 / Math.Sqrt(2.0), 0.0);
            double  e_inc_factor = 1.0 / 4.0; // Factor that increases or decreases incident intensity
            Complex e_inc        = new Complex(1.0 * Math.Sqrt(e_inc_factor), 0.0);

            int    det_rows = 1, det_cols = 1;
            double temperature = 325.15;
            //double pga_gain = 2.0;
            //double integration_time = 0.035;
            double pga_gain         = 1.0 * 4.0;  // Gain of the image sensor
            double integration_time = 1.0 / 25.0; //0.040 / 128.0;

            // Maximum photon shot noise without the influence of dark current
            // e_inc_factor = 8.0 or 9.0
            // pga_gain = 64.0
            // integration_time = 1.0 / 8000.0

            // Maximum dark current
            // e_inc_factor = 1.0 / 4.0
            // pga_gain = 4.0
            // integration_time = 1.0 / 25.0

            double planck_const = 6.62607004e-34;
            double light_speed  = 299792458.0;
            double ph_energy    = planck_const * light_speed / wavelength; // Photon energy
            double poynting_vec = e_inc.MagnitudeSquared() / (2.0 * 377);
            double ph_flux      = poynting_vec / ph_energy;                // Photon flux

            double q_eff            = 0.8;
            double det_area         = 5.0e-6 * 5.0e-6;
            double sensor_electrons = ph_flux * q_eff * det_area * integration_time;

            double well_capacity = 20.0e3;
            double intensity_dn  = sensor_electrons / well_capacity * 255.0;


            Console.WriteLine("Photon flux [1/(m^2 s)]: {0:E4}", ph_flux);
            Console.WriteLine("Generated electrons:     {0}", Math.Round(sensor_electrons, 0));
            Console.WriteLine("Intensity [DN]:          {0}", Math.Round(intensity_dn, 0));

            // Create functions that calculate intensity directly and simulate an image sensor
            Func <double, double> ideal_detector = new Func <double, double>((input_intensity) => {
                return(input_intensity);
                //double poynting_vec_local = input_intensity / (2.0 * 377.0);
                //double ph_flux_local = poynting_vec_local / ph_energy;
                //double sensor_electrons_local = ph_flux_local * q_eff * det_area * integration_time;
                //double intensity_dn_local = sensor_electrons_local / well_capacity * 255.0;
                //return intensity_dn_local;
            });
            Func <double, double> experimental_detector = new Func <double, double>((input_intensity) => {
                //double poynting_vec_local = input_intensity / (2.0 * 377.0);
                //double ph_flux_local = poynting_vec_local / ph_energy;
                //double sensor_eklectrons = ph_flux_local * q_eff * det_area * integration_time;
                //if (sensor_eklectrons == 0.0)
                //    return 0.0;
                //else if (sensor_eklectrons < 1000.0)
                //    return Poisson.Sample(sensor_eklectrons);
                //else
                //    return Normal.Sample(sensor_eklectrons, Math.Sqrt(sensor_eklectrons));

                double signal = Normal.Sample(input_intensity, 1.0);
                if (signal < 0.0)
                {
                    return(0.0);
                }
                else
                {
                    return(signal);
                }

                //return ContinuousUniform.Sample(0.1, 1.0);
            });
            ImageSensor sensor_w_noise = ImageSensorConstructor.CustomSensor(
                det_rows, det_cols, temperature, pga_gain, integration_time, true);
            Func <double, double> advanced_detector = new Func <double, double>((input_intensity) => {
                double poynting_vec_local = input_intensity / (2.0 * 377.0);
                double ph_flux_local      = poynting_vec_local / ph_energy;
                return(SimulateCamera(sensor_w_noise, ph_flux_local));
            });
            ImageSensor sensor_wo_noise = ImageSensorConstructor.CustomSensor(
                det_rows, det_cols, temperature, pga_gain, integration_time, false);
            Func <double, double> advanced_detector_noiseless = new Func <double, double>((input_intensity) => {
                double poynting_vec_local = input_intensity / (2.0 * 377.0);
                double ph_flux_local      = poynting_vec_local / ph_energy;
                return(SimulateCamera(sensor_wo_noise, ph_flux_local));
            });

            //double conversion_coefficient = SimulateCamera(sensor_wo_noise, ph_flux);
            double conversion_coefficient = advanced_detector_noiseless(e_inc.MagnitudeSquared());

            Console.WriteLine("Conversion coefficient: {0}", conversion_coefficient);
            Console.WriteLine("w/o noise: {0}", advanced_detector_noiseless(e_inc.MagnitudeSquared()));
            Console.WriteLine("w/  noise: {0}", advanced_detector(e_inc.MagnitudeSquared()));
            //for (int i = 0; i < 100; i++)
            //    Console.WriteLine(advanced_detector(e_inc.MagnitudeSquared()));
            ImageSensor sensor_w_noise_tp = ImageSensorConstructor.CustomSensor(
                512, 512, temperature, pga_gain, integration_time, true);
            ImageSensor sensor_wo_noise_tp = ImageSensorConstructor.CustomSensor(
                512, 512, temperature, pga_gain, integration_time, false);

            ImageSensorConstructor.GenerateTestPattern(
                sensor_w_noise_tp, 2.0 * e_inc.MagnitudeSquared() / (2.0 * 377.0) / ph_energy)
            .WriteToBinary("d:/Wavefront shaping/Tasks/01_CCD_noise_Wiener_filter/sensor_w_noise_tp.raw");
            ImageSensorConstructor.GenerateTestPattern(
                sensor_wo_noise_tp, 2.0 * e_inc.MagnitudeSquared() / (2.0 * 377.0) / ph_energy)
            .WriteToBinary("d:/Wavefront shaping/Tasks/01_CCD_noise_Wiener_filter/sensor_wo_noise_tp.raw");

            // Create a set of transmission matrices for Wiener filter calculation
            Console.Write("Preparing transmission vectors... ");
            MathNet.Numerics.LinearAlgebra.Vector <Complex>[] t_vectors =
                TransmissionMatrix.GenerateTransmissionVectorsArray(slm_size, t_vecs_count, true);

            //List<mn_linalg::Vector<Complex>> t_vectors_filtered = new List<mn_linalg.Vector<Complex>>(t_vectors.Length);
            //for (int i = 0; i < t_vectors.Length; i++)
            //{
            //    if (experimental_detector((t_vectors[i].Sum() * e_inc).MagnitudeSquared()) != 0.0)
            //    {
            //        t_vectors_filtered.Add(t_vectors[i]);
            //    }
            //}
            //t_vectors = t_vectors_filtered.ToArray();

            //for (int i = 0; i < t_vectors.Length; i++)
            //    Console.WriteLine((t_vectors[i] * t_vectors[i].Conjugate()).MagnitudeSquared());
            Console.WriteLine("DONE");

            Console.Write("Preparing transmission vectors for filter verification... ");
            MathNet.Numerics.LinearAlgebra.Vector <Complex>[] t_vectors_fverif =
                TransmissionMatrix.GenerateTransmissionVectorsArray(slm_size, t_vecs_count, true);
            Console.WriteLine("DONE");

            //mn_linalg::Vector<Complex>[] t_vectors_all =
            //    TransmissionMatrix.GenerateTransmissionVectorsArray(slm_size, 2 * t_vecs_count, true);
            //t_vectors = t_vectors_all.Take(t_vecs_count).ToArray();
            //t_vectors_fverif = t_vectors_all.Skip(t_vecs_count).Take(t_vecs_count).ToArray();


            //List<mn_linalg::Vector<Complex>> t_vectors_fverif_filtered = new List<mn_linalg.Vector<Complex>>(t_vectors_fverif.Length);
            //for (int i = 0; i < t_vectors_fverif.Length; i++)
            //{
            //    if (experimental_detector((t_vectors_fverif[i].Sum() * e_inc).MagnitudeSquared()) != 0.0)
            //    {
            //        t_vectors_fverif_filtered.Add(t_vectors_fverif[i]);
            //    }
            //}
            //t_vectors_fverif = t_vectors_fverif_filtered.ToArray();

            Analyzer a1 = new Analyzer(experimental_detector);

            a1.Analyze(t_vectors, t_vectors_fverif, e_inc, true);
            a1.PrintResults();

            a1.Filter.GammaZeta.WriteToBinary("d:/Wavefront shaping/Tasks/01_CCD_noise_Wiener_filter/gamma_zeta.raw");
            a1.Filter.GammaZeta.Svd().W.Diagonal().WriteToCSV("d:/Wavefront shaping/Tasks/01_CCD_noise_Wiener_filter/gamma_zeta_sing_vals.csv");
            a1.Filter.GammaZetaEig.WriteToCSV("d:/Wavefront shaping/Tasks/01_CCD_noise_Wiener_filter/gamma_zeta_eig.csv");

            string line = "";
            int    num  = 0;

            while (line != "q")
            {
                Console.Write("Number of a vector to save or q tu quit: ");
                line = Console.ReadLine();
                if (!int.TryParse(line, out num))
                {
                    continue;
                }
                t_vectors[num].WriteToCSV("d:/Wavefront shaping/Tasks/01_CCD_noise_Wiener_filter/t_vector_original.csv");
                a1.TransmissionVectorsFiltered[num].WriteToCSV(
                    "d:/Wavefront shaping/Tasks/01_CCD_noise_Wiener_filter/t_vector_filtered.csv");
                a1.HResults[num].TransmissionVectorEstimated.WriteToCSV(
                    "d:/Wavefront shaping/Tasks/01_CCD_noise_Wiener_filter/t_vector_estimated.csv");
            }

            return;

            Console.Write("Simulating focusing... ");
            HadamardResult[] hr_ideal_detector        = HadamardAlgorithm.BatchSimulate(ideal_detector, t_vectors, e_inc);
            HadamardResult[] hr_sensor_w_noise        = HadamardAlgorithm.BatchSimulate(advanced_detector, t_vectors, e_inc, true);
            HadamardResult[] hr_sensor_w_noise_fverif = HadamardAlgorithm.BatchSimulate(advanced_detector, t_vectors_fverif, e_inc, true);
            HadamardResult[] hr_sensor_wo_noise       = HadamardAlgorithm.BatchSimulate(advanced_detector_noiseless, t_vectors, e_inc, true);
            Console.WriteLine("DONE");

            Console.Write("Scaling output... ");
            for (int i = 0; i < hr_sensor_wo_noise.Length; i++)
            {
                hr_ideal_detector[i].TransmissionVectorEstimated /= Math.Sqrt(e_inc_factor);
                hr_ideal_detector[i].Zeta /= Math.Sqrt(e_inc_factor);
                hr_sensor_wo_noise[i].TransmissionVectorEstimated /= Math.Sqrt(conversion_coefficient);
                hr_sensor_wo_noise[i].Zeta /= Math.Sqrt(conversion_coefficient);
                hr_sensor_w_noise[i].TransmissionVectorEstimated /= Math.Sqrt(conversion_coefficient);
                hr_sensor_w_noise[i].Zeta /= Math.Sqrt(conversion_coefficient);

                hr_sensor_w_noise_fverif[i].TransmissionVectorEstimated /= Math.Sqrt(conversion_coefficient);
                hr_sensor_w_noise_fverif[i].Zeta /= Math.Sqrt(conversion_coefficient);
            }
            Console.WriteLine("DONE");

            // Build Wiener filters
            Console.Write("Building Wiener filters... ");
            WienerFilter wf_ideal_detector  = new WienerFilter(hr_ideal_detector, t_vectors);
            WienerFilter wf_sensor_w_noise  = new WienerFilter(hr_sensor_w_noise, t_vectors);
            WienerFilter wf_sensor_wo_noise = new WienerFilter(hr_sensor_wo_noise, t_vectors);

            Console.WriteLine("DONE");

            Console.Write("Filtering input data... ");
            MathNet.Numerics.LinearAlgebra.Vector <double>[] t_vectors_est_ideal_detector =
                wf_ideal_detector.BatchApply(hr_ideal_detector.Select(e => e.Zeta.Real()).ToArray());
            //MathNet.Numerics.LinearAlgebra.Vector<double>[] t_vectors_est_sensor_w_noise =
            //    wf_sensor_w_noise.BatchApply(hr_ideal_detector.Select(e => e.Zeta.Real()).ToArray());
            //MathNet.Numerics.LinearAlgebra.Vector<double>[] t_vectors_est_sensor_wo_noise =
            //    wf_sensor_wo_noise.BatchApply(hr_ideal_detector.Select(e => e.Zeta.Real()).ToArray());
            MathNet.Numerics.LinearAlgebra.Vector <double>[] t_vectors_est_sensor_w_noise =
                wf_sensor_w_noise.BatchApply(hr_sensor_w_noise.Select(e => e.Zeta.Real()).ToArray());
            MathNet.Numerics.LinearAlgebra.Vector <double>[] t_vectors_est_sensor_wo_noise =
                wf_sensor_wo_noise.BatchApply(hr_sensor_wo_noise.Select(e => e.Zeta.Real()).ToArray());

            MathNet.Numerics.LinearAlgebra.Vector <double>[] t_vectors_est_sensor_w_noise_fverif =
                wf_sensor_w_noise.BatchApply(hr_sensor_w_noise_fverif.Select(e => e.Zeta.Real()).ToArray());
            Console.WriteLine("DONE");

            Console.Write("Calculating correct estimation statistics... ");
            double hr_ideal_detector_cs =
                hr_ideal_detector.Average(new Func <HadamardResult, double>(e => (double)e.CorrectSignsNumber));
            double hr_sensor_w_noise_cs =
                hr_sensor_w_noise.Average(new Func <HadamardResult, double>(e => (double)e.CorrectSignsNumber));
            double hr_sensor_wo_noise_cs =
                hr_sensor_wo_noise.Average(new Func <HadamardResult, double>(e => (double)e.CorrectSignsNumber));

            double hr_sensor_w_noise_cs_fverif =
                hr_sensor_w_noise_fverif.Average(new Func <HadamardResult, double>(e => (double)e.CorrectSignsNumber));

            double hr_ideal_detector_cs_f  = 0.0;
            double hr_sensor_w_noise_cs_f  = 0.0;
            double hr_sensor_wo_noise_cs_f = 0.0;

            double hr_sensor_w_noise_cs_ffverif = 0.0;

            for (int i = 0; i < t_vectors.Length; i++)
            {
                for (int j = 0; j < t_vectors[j].Count; j++)
                {
                    if (Math.Sign(t_vectors_est_sensor_w_noise[i][j]) == Math.Sign(t_vectors[i][j].Real))
                    {
                        hr_sensor_w_noise_cs_f += 1.0;
                    }
                    if (Math.Sign(t_vectors_est_sensor_wo_noise[i][j]) == Math.Sign(t_vectors[i][j].Real))
                    {
                        hr_sensor_wo_noise_cs_f += 1.0;
                    }
                    if (Math.Sign(t_vectors_est_ideal_detector[i][j]) == Math.Sign(t_vectors[i][j].Real))
                    {
                        hr_ideal_detector_cs_f += 1.0;
                    }

                    if (Math.Sign(t_vectors_est_sensor_w_noise_fverif[i][j]) == Math.Sign(t_vectors_fverif[i][j].Real))
                    {
                        hr_sensor_w_noise_cs_ffverif += 1.0;
                    }
                }
            }
            hr_ideal_detector_cs_f       /= t_vectors.Length;
            hr_sensor_w_noise_cs_f       /= t_vectors.Length;
            hr_sensor_wo_noise_cs_f      /= t_vectors.Length;
            hr_sensor_w_noise_cs_ffverif /= t_vectors_fverif.Length;


            // Count wrong to right and right to wrong estimations
            double hr_ideal_detector_cs_rtw  = 0.0;
            double hr_ideal_detector_cs_wtr  = 0.0;
            double hr_sensor_w_noise_cs_rtw  = 0.0;
            double hr_sensor_w_noise_cs_wtr  = 0.0;
            double hr_sensor_wo_noise_cs_rtw = 0.0;
            double hr_sensor_wo_noise_cs_wtr = 0.0;

            double hr_sensor_w_noise_cs_fverif_rtw = 0.0;
            double hr_sensor_w_noise_cs_fverif_wtr = 0.0;

            for (int i = 0; i < t_vectors.Length; i++)
            {
                for (int j = 0; j < t_vectors[j].Count; j++)
                {
                    if (Math.Sign(hr_ideal_detector[i].TransmissionVectorEstimated[j].Real) != Math.Sign(t_vectors[i][j].Real) &&
                        Math.Sign(t_vectors_est_ideal_detector[i][j]) == Math.Sign(t_vectors[i][j].Real))
                    {
                        hr_ideal_detector_cs_wtr += 1.0;
                    }
                    else if (Math.Sign(hr_ideal_detector[i].TransmissionVectorEstimated[j].Real) == Math.Sign(t_vectors[i][j].Real) &&
                             Math.Sign(t_vectors_est_ideal_detector[i][j]) != Math.Sign(t_vectors[i][j].Real))
                    {
                        hr_ideal_detector_cs_rtw += 1.0;
                    }

                    if (Math.Sign(hr_sensor_wo_noise[i].TransmissionVectorEstimated[j].Real) != Math.Sign(t_vectors[i][j].Real) &&
                        Math.Sign(t_vectors_est_sensor_wo_noise[i][j]) == Math.Sign(t_vectors[i][j].Real))
                    {
                        hr_sensor_wo_noise_cs_wtr += 1.0;
                    }
                    else if (Math.Sign(hr_sensor_wo_noise[i].TransmissionVectorEstimated[j].Real) == Math.Sign(t_vectors[i][j].Real) &&
                             Math.Sign(t_vectors_est_sensor_wo_noise[i][j]) != Math.Sign(t_vectors[i][j].Real))
                    {
                        hr_sensor_wo_noise_cs_rtw += 1.0;
                    }

                    if (Math.Sign(hr_sensor_w_noise[i].TransmissionVectorEstimated[j].Real) != Math.Sign(t_vectors[i][j].Real) &&
                        Math.Sign(t_vectors_est_sensor_w_noise[i][j]) == Math.Sign(t_vectors[i][j].Real))
                    {
                        hr_sensor_w_noise_cs_wtr += 1.0;
                    }
                    else if (Math.Sign(hr_sensor_w_noise[i].TransmissionVectorEstimated[j].Real) == Math.Sign(t_vectors[i][j].Real) &&
                             Math.Sign(t_vectors_est_sensor_w_noise[i][j]) != Math.Sign(t_vectors[i][j].Real))
                    {
                        hr_sensor_w_noise_cs_rtw += 1.0;
                    }


                    if (Math.Sign(hr_sensor_w_noise_fverif[i].TransmissionVectorEstimated[j].Real) != Math.Sign(t_vectors_fverif[i][j].Real) &&
                        Math.Sign(t_vectors_est_sensor_w_noise_fverif[i][j]) == Math.Sign(t_vectors_fverif[i][j].Real))
                    {
                        hr_sensor_w_noise_cs_fverif_wtr += 1.0;
                    }
                    else if (Math.Sign(hr_sensor_w_noise_fverif[i].TransmissionVectorEstimated[j].Real) == Math.Sign(t_vectors_fverif[i][j].Real) &&
                             Math.Sign(t_vectors_est_sensor_w_noise_fverif[i][j]) != Math.Sign(t_vectors_fverif[i][j].Real))
                    {
                        hr_sensor_w_noise_cs_fverif_rtw += 1.0;
                    }
                }
            }

            MathNet.Numerics.LinearAlgebra.Vector <Complex>[] slm_sensor_w_noise = t_vectors_est_sensor_w_noise.Select(e => {
                MathNet.Numerics.LinearAlgebra.Vector <Complex> res =
                    MathNet.Numerics.LinearAlgebra.Vector <Complex> .Build.Dense(e.Count);
                for (int i = 0; i < e.Count; i++)
                {
                    if (e[i] >= 0.0)
                    {
                        res[i] = 1.0;
                    }
                }
                return(res);
            }).ToArray();
            MathNet.Numerics.LinearAlgebra.Vector <Complex>[] slm_sensor_wo_noise = t_vectors_est_sensor_wo_noise.Select(e => {
                MathNet.Numerics.LinearAlgebra.Vector <Complex> res =
                    MathNet.Numerics.LinearAlgebra.Vector <Complex> .Build.Dense(e.Count);
                for (int i = 0; i < e.Count; i++)
                {
                    if (e[i] >= 0.0)
                    {
                        res[i] = 1.0;
                    }
                }
                return(res);
            }).ToArray();

            MathNet.Numerics.LinearAlgebra.Vector <Complex>[] slm_sensor_w_noise_fverif = t_vectors_est_sensor_w_noise_fverif.Select(e => {
                MathNet.Numerics.LinearAlgebra.Vector <Complex> res =
                    MathNet.Numerics.LinearAlgebra.Vector <Complex> .Build.Dense(e.Count);
                for (int i = 0; i < e.Count; i++)
                {
                    if (e[i] >= 0.0)
                    {
                        res[i] = 1.0;
                    }
                }
                return(res);
            }).ToArray();

            double ideal_detector_opt_int    = 0.0;
            double sensor_w_noise_opt_int    = 0.0;
            double sensor_w_noise_f_opt_int  = 0.0;
            double sensor_wo_noise_opt_int   = 0.0;
            double sensor_wo_noise_f_opt_int = 0.0;

            double sensor_w_noise_opt_int_fverif   = 0.0;
            double sensor_w_noise_f_opt_int_fverif = 0.0;

            for (int i = 0; i < t_vectors.Length; i++)
            {
                ideal_detector_opt_int    += ideal_detector(((t_vectors[i] * e_inc) * hr_ideal_detector[i].SLMPatternOptimized).MagnitudeSquared());
                sensor_w_noise_opt_int    += ideal_detector(((t_vectors[i] * e_inc) * hr_sensor_w_noise[i].SLMPatternOptimized).MagnitudeSquared());
                sensor_w_noise_f_opt_int  += ideal_detector(((t_vectors[i] * e_inc) * slm_sensor_w_noise[i]).MagnitudeSquared());
                sensor_wo_noise_f_opt_int += ideal_detector(((t_vectors[i] * e_inc) * slm_sensor_wo_noise[i]).MagnitudeSquared());
                sensor_wo_noise_opt_int   += ideal_detector(((t_vectors[i] * e_inc) * hr_sensor_wo_noise[i].SLMPatternOptimized).MagnitudeSquared());

                sensor_w_noise_opt_int_fverif   += ideal_detector(((t_vectors_fverif[i] * e_inc) * hr_sensor_w_noise_fverif[i].SLMPatternOptimized).MagnitudeSquared());
                sensor_w_noise_f_opt_int_fverif += ideal_detector(((t_vectors_fverif[i] * e_inc) * slm_sensor_w_noise_fverif[i]).MagnitudeSquared());
            }
            ideal_detector_opt_int    /= t_vecs_count;
            sensor_w_noise_opt_int    /= t_vecs_count;
            sensor_w_noise_f_opt_int  /= t_vecs_count;
            sensor_wo_noise_f_opt_int /= t_vecs_count;
            sensor_wo_noise_opt_int   /= t_vecs_count;

            sensor_w_noise_opt_int_fverif   /= t_vecs_count;
            sensor_w_noise_f_opt_int_fverif /= t_vecs_count;

            double sensor_w_noise_avg_int =
                hr_sensor_w_noise.Average(new Func <HadamardResult, double>(e => (e.IntensityPlus.Average() + e.IntensityMinus.Average()) / 2.0));
            double sensor_w_noise_max_int =
                hr_sensor_w_noise.Average(new Func <HadamardResult, double>(e => Math.Max(e.IntensityPlus.Max(), e.IntensityMinus.Max())));
            double sensor_w_noise_min_int =
                hr_sensor_w_noise.Average(new Func <HadamardResult, double>(e => Math.Max(e.IntensityPlus.Min(), e.IntensityMinus.Min())));

            double sensor_wo_noise_avg_int =
                hr_sensor_wo_noise.Average(new Func <HadamardResult, double>(e => (e.IntensityPlus.Average() + e.IntensityMinus.Average()) / 2.0));
            double sensor_wo_noise_max_int =
                hr_sensor_wo_noise.Average(new Func <HadamardResult, double>(e => Math.Max(e.IntensityPlus.Max(), e.IntensityMinus.Max())));
            double sensor_wo_noise_min_int =
                hr_sensor_wo_noise.Average(new Func <HadamardResult, double>(e => Math.Max(e.IntensityPlus.Min(), e.IntensityMinus.Min())));


            double sensor_w_noise_avg_int_fverif =
                hr_sensor_w_noise_fverif.Average(new Func <HadamardResult, double>(e => (e.IntensityPlus.Average() + e.IntensityMinus.Average()) / 2.0));
            double sensor_w_noise_max_int_fverif =
                hr_sensor_w_noise_fverif.Average(new Func <HadamardResult, double>(e => Math.Max(e.IntensityPlus.Max(), e.IntensityMinus.Max())));
            double sensor_w_noise_min_int_fverif =
                hr_sensor_w_noise_fverif.Average(new Func <HadamardResult, double>(e => Math.Max(e.IntensityPlus.Min(), e.IntensityMinus.Min())));

            Console.WriteLine("DONE");

            Console.WriteLine("- Ideal detector");
            Console.WriteLine("WtR - RtW switches:                   {0}", hr_ideal_detector_cs_wtr - hr_ideal_detector_cs_rtw);
            Console.WriteLine("Optimized intensity:                  {0:E4}", ideal_detector_opt_int);
            //Console.WriteLine("Optimized intensity (filtered): {0:E4}", ideal_detector_opt_int);
            Console.WriteLine("Correct estimations:                  {0}", hr_ideal_detector_cs);
            Console.WriteLine("- Sensor w/o noise");
            Console.WriteLine("Correct estimations:                  {0}", hr_sensor_wo_noise_cs);
            Console.WriteLine("Correct estimations (filtered):       {0}", hr_sensor_wo_noise_cs_f);
            Console.WriteLine("WtR - RtW switches:                   {0}", hr_sensor_wo_noise_cs_wtr - hr_sensor_wo_noise_cs_rtw);
            Console.WriteLine("Optimized intensity (true):           {0:E4}", sensor_wo_noise_opt_int);
            Console.WriteLine("Optimized intensity (true, filtered): {0:E4}", sensor_wo_noise_f_opt_int);
            Console.WriteLine("Average intensity (camera):           {0}", Math.Round(sensor_wo_noise_avg_int, 0));
            Console.WriteLine("Minimal intensity (camera):           {0}", Math.Round(sensor_wo_noise_min_int, 0));
            Console.WriteLine("Maximal intensity (camera):           {0}", Math.Round(sensor_wo_noise_max_int, 0));
            Console.WriteLine("- Sensor w/ noise");
            Console.WriteLine("Correct estimations:                  {0}", hr_sensor_w_noise_cs);
            Console.WriteLine("Correct estimations (filtered):       {0}", hr_sensor_w_noise_cs_f);
            Console.WriteLine("WtR - RtW switches:                   {0}", hr_sensor_w_noise_cs_wtr - hr_sensor_w_noise_cs_rtw);
            Console.WriteLine("Optimized intensity (true):           {0:E4}", sensor_w_noise_opt_int);
            Console.WriteLine("Optimized intensity (true, filtered): {0:E4}", sensor_w_noise_f_opt_int);
            Console.WriteLine("Average intensity (camera):           {0}", Math.Round(sensor_w_noise_avg_int, 0));
            Console.WriteLine("Minimal intensity (camera):           {0}", Math.Round(sensor_w_noise_min_int, 0));
            Console.WriteLine("Maximal intensity (camera):           {0}", Math.Round(sensor_w_noise_max_int, 0));

            Console.WriteLine("- Sensor w/ noise (verification)");
            Console.WriteLine("Correct estimations:                  {0}", hr_sensor_w_noise_cs_fverif);
            Console.WriteLine("Correct estimations (filtered):       {0}", hr_sensor_w_noise_cs_ffverif);
            Console.WriteLine("WtR - RtW switches:                   {0}", hr_sensor_w_noise_cs_fverif_wtr - hr_sensor_w_noise_cs_fverif_rtw);
            Console.WriteLine("Optimized intensity (true):           {0:E4}", sensor_w_noise_opt_int_fverif);
            Console.WriteLine("Optimized intensity (true, filtered): {0:E4}", sensor_w_noise_f_opt_int_fverif);
            Console.WriteLine("Average intensity (camera):           {0}", Math.Round(sensor_w_noise_avg_int_fverif, 0));
            Console.WriteLine("Minimal intensity (camera):           {0}", Math.Round(sensor_w_noise_min_int_fverif, 0));
            Console.WriteLine("Maximal intensity (camera):           {0}", Math.Round(sensor_w_noise_max_int_fverif, 0));

            hr_ideal_detector[0].TransmissionVectorEstimated.Real().WriteToCSV(
                "d:/Wavefront shaping/Tasks/01_CCD_noise_Wiener_filter/ideal_detector.csv");
            hr_sensor_w_noise[0].TransmissionVectorEstimated.Real().WriteToCSV(
                "d:/Wavefront shaping/Tasks/01_CCD_noise_Wiener_filter/sensor_w_noise.csv");
            t_vectors_est_sensor_w_noise[0].WriteToCSV(
                "d:/Wavefront shaping/Tasks/01_CCD_noise_Wiener_filter/sensor_w_noise_filtered.csv");
            hr_sensor_wo_noise[0].TransmissionVectorEstimated.Real().WriteToCSV(
                "d:/Wavefront shaping/Tasks/01_CCD_noise_Wiener_filter/sensor_wo_noise.csv");
        }
Exemplo n.º 8
0
 /// <summary>
 /// 新建一个相机
 /// </summary>
 /// <param name="length">传感器长度</param>
 /// <param name="width">传感器宽度</param>
 /// <param name="f">焦距</param>
 /// <param name="name">相机名称</param>
 public Camera(int length, int width, int f, string name = "Default")
 {
     Name   = name;
     Sensor = new ImageSensor(length, width);
     Focal  = f;
 }
Exemplo n.º 9
0
 /// <summary>
 /// 新建一个相机
 /// </summary>
 /// <param name="sensorEnum">传感器类型</param>
 /// <param name="f">焦距</param>
 /// <param name="name">相机名称</param>
 public Camera(ImageSensorTyprEnum sensorEnum, int f, string name = "Default")
 {
     Name   = name;
     Sensor = new ImageSensor(sensorEnum);
     Focal  = f;
 }