private MWNumericArray[][] wrap_func(int num, int ray_num, Transmitter.EMIT_OPTION emit_opt, Signal_Type signal_type, int sample_cnt) { MWNumericArray[][] signals = new MWNumericArray[num][]; while (num-- > 0) { foreach (Transmitter trans in this.viz_rps.Map.Transmitters) { trans.Reset(); trans.EmitRays(ray_num, emit_opt); //各发射机辐射波束 foreach (Receiver rec in this.viz_rps.Map.Receivers) { rec.Reset(); if (this.viz_rps.Map.IsLOS(trans.Location, rec.Location)) //若存在LOS情况 { LineSegment segLOS = new LineSegment(trans.Location, rec.Location); Ray rayLOS = new Ray(trans, new LineSegment[] { segLOS }, segLOS.DirectionRadian, trans.CenterFrequency, trans.EmitPower * trans.Gain, trans.EmitPower * trans.Gain, true); rec.AcceptRay(rayLOS, this.viz_rps.Map); } } foreach (Ray ray in trans.Rays) //更新每条ray, 若被接收,则更新接收它的接收机 { Receiver rec = null; while (!ray.Update(this.viz_rps.Map, out rec)) ; if (rec != null) rec.AcceptRay(ray, this.viz_rps.Map); } } FingerprintGenerator fg = new FingerprintGenerator(); Dictionary<Fingerprint_Name, Fingerprint> result = new Dictionary<Fingerprint_Name, Fingerprint>(); List<double>[] ray_angles = new List<double>[this.viz_rps.Map.ReceiverCount]; List<double>[] ray_strengths = new List<double>[this.viz_rps.Map.ReceiverCount]; List<double>[] ray_taus = new List<double>[this.viz_rps.Map.ReceiverCount]; int kk = 0; foreach (Receiver rec in this.viz_rps.Map.Receivers) { ray_angles[kk] = new List<double>(); ray_strengths[kk] = new List<double>(); ray_taus[kk] = new List<double>(); foreach (Ray ray in rec.Rays) { ray_angles[kk].Add(ray.CurrentDirection); ray_strengths[kk].Add(ray.FadedPower(this.viz_rps.Map.MeterPixelRatio)); ray_taus[kk].Add(ray.GetLifeSpan(this.viz_rps.Map.MeterPixelRatio)); } kk++; } double[] args = null; switch (signal_type) { case Signal_Type.PSEUDO_DOPPLER: //if (this.receivers.First().Value.Antenna.AntType != AntennaType.PSEUDO_DOPPLER) // return null; Antenna_PseudoDoppler ant = this.viz_rps.Map.receivers.First().Value.Antenna as Antenna_PseudoDoppler; args = fg.PackArgs_PseudoDoppler(this.viz_rps.Map.receivers.First().Value.FrequencyLow, this.viz_rps.Map.receivers.First().Value.CenterFrequency, this.viz_rps.Map.receivers.First().Value.SampleFrequency, ant.SwitchFrequency, ant.Radius, ant.AntennaNumber); break; } signals[num - 1] = new MWNumericArray[ray_angles.GetLength(0)]; for (int i = 0; i < signals.Length; i++) { signals[num - 1][i] = new FingerprintGenerator().GenerateSignal_MW(signal_type, ray_angles[i].ToArray(), ray_strengths[i].ToArray(), ray_taus[i].ToArray(), sample_cnt, args); } } return signals; }
private void AutoTest(object sender, DoWorkEventArgs e) { object[] param = (object[])e.Argument; int avg_cnt = (int)param[0]; int KNN_K = (int)param[1]; int KNNHC_K = (int)param[2]; string db_file_name = (string)param[3]; string data_file_name = (string)param[4]; double start_snr = (double)param[5]; double stop_snr = (double)param[6]; double step_snr = (double)param[7]; int ray_num = (int)param[8]; Transmitter.EMIT_OPTION emit_opt = (Transmitter.EMIT_OPTION)param[9]; Noise_Type noise_type = (Noise_Type)param[10]; double alpha = (double)param[11]; int sample_cnt = (int)param[12]; Signal_Type signal_type = (Signal_Type)param[13]; System.IO.FileStream data_fs = new FileStream(data_file_name, FileMode.Create, FileAccess.Write, FileShare.Read); System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); FileStream fs = new FileStream(db_file_name, FileMode.Open, FileAccess.Read, FileShare.Read); FingerprintDataBase fdb = (FingerprintDataBase)formatter.Deserialize(fs); fs.Close(); Point pt_real = new Point(); foreach (Transmitter trans in this.viz_rps.Map.Transmitters) pt_real = trans.Location; double[] snrs = new double[(int)((stop_snr - start_snr) / step_snr) + 1]; for(int i = 0; i < snrs.Length; i++) snrs[i] = start_snr + i * step_snr; foreach (Transmitter trans in this.viz_rps.Map.Transmitters) { trans.Reset(); trans.EmitRays(ray_num, emit_opt); //各发射机辐射波束 foreach (Receiver rec in this.viz_rps.Map.Receivers) { rec.Reset(); if (this.viz_rps.Map.IsLOS(trans.Location, rec.Location)) //若存在LOS情况 { LineSegment segLOS = new LineSegment(trans.Location, rec.Location); Ray rayLOS = new Ray(trans, new LineSegment[] { segLOS }, segLOS.DirectionRadian, trans.CenterFrequency, trans.EmitPower * trans.Gain, trans.EmitPower * trans.Gain, true); rec.AcceptRay(rayLOS, this.viz_rps.Map); } } foreach (Ray ray in trans.Rays) //更新每条ray, 若被接收,则更新接收它的接收机 { Receiver rec = null; while (!ray.Update(this.viz_rps.Map, out rec)) ; if (rec != null) rec.AcceptRay(ray, this.viz_rps.Map); } } FingerprintGenerator fg = new FingerprintGenerator(); Dictionary<Fingerprint_Name, Fingerprint> result = new Dictionary<Fingerprint_Name, Fingerprint>(); List<double>[] ray_angles = new List<double>[this.viz_rps.Map.ReceiverCount]; List<double>[] ray_strengths = new List<double>[this.viz_rps.Map.ReceiverCount]; List<double>[] ray_taus = new List<double>[this.viz_rps.Map.ReceiverCount]; int kk = 0; foreach (Receiver rec in this.viz_rps.Map.Receivers) { ray_angles[kk] = new List<double>(); ray_strengths[kk] = new List<double>(); ray_taus[kk] = new List<double>(); foreach (Ray ray in rec.Rays) { ray_angles[kk].Add(ray.CurrentDirection); ray_strengths[kk].Add(ray.FadedPower(this.viz_rps.Map.MeterPixelRatio)); ray_taus[kk].Add(ray.GetLifeSpan(this.viz_rps.Map.MeterPixelRatio)); } kk++; } double[] args = null; switch (signal_type) { case Signal_Type.PSEUDO_DOPPLER: //if (this.receivers.First().Value.Antenna.AntType != AntennaType.PSEUDO_DOPPLER) // return null; Antenna_PseudoDoppler ant = this.viz_rps.Map.receivers.First().Value.Antenna as Antenna_PseudoDoppler; args = fg.PackArgs_PseudoDoppler(this.viz_rps.Map.receivers.First().Value.FrequencyLow, this.viz_rps.Map.receivers.First().Value.CenterFrequency, this.viz_rps.Map.receivers.First().Value.SampleFrequency, ant.SwitchFrequency, ant.Radius, ant.AntennaNumber); break; } MWNumericArray[] signals = new MWNumericArray[ray_angles.GetLength(0)]; for (int i = 0; i < signals.Length; i++) { signals[i] = new FingerprintGenerator().GenerateSignal_MW(signal_type, ray_angles[i].ToArray(), ray_strengths[i].ToArray(), ray_taus[i].ToArray(), sample_cnt, args); } for (int n = 0; n < snrs.Length; n++) { double[] sum_errs = new double[6];//SA_single, SA_KNN, SA_KNNHC, S_single, S_KNN, S_KNNHC for(int i = 0; i < avg_cnt; i++) { MWNumericArray[] n_signals = new MWNumericArray[signals.Length]; for (int l = 0; l < signals.Length; l++) { n_signals[l] = new FingerprintGenerator().AddNoise_MW(noise_type, signals[l], new double[] { snrs[n] }); } Dictionary<Fingerprint_Name, Fingerprint> res = new Dictionary<Fingerprint_Name,Fingerprint>(); for (int j = 0; j < Enum.GetNames(new Fingerprint_Name().GetType()).Length; j++ ) res[(Fingerprint_Name)j] = new FingerprintGenerator().GenerateFingerprint(signal_type, (Fingerprint_Name)j, n_signals, args); this.current_fingerprint = res; //SA_single PointF loc_SA_single = fdb.MatchLocation(this.current_fingerprint[Fingerprint_Name.SA], FingerprintDataBase.Match_Strategy.Single, null); sum_errs[0] += new MathUtils.MathUtility().point_dist(pt_real, loc_SA_single); //SA_KNN PointF loc_SA_KNN = fdb.MatchLocation(this.current_fingerprint[Fingerprint_Name.SA], FingerprintDataBase.Match_Strategy.KNN, new double[] { KNN_K }); sum_errs[1] += new MathUtils.MathUtility().point_dist(pt_real, loc_SA_KNN); //SA_KNNHC PointF loc_SA_KNNHC = fdb.MatchLocation(this.current_fingerprint[Fingerprint_Name.SA], FingerprintDataBase.Match_Strategy.KNN_HC, new double[] { KNNHC_K }); sum_errs[2] += new MathUtils.MathUtility().point_dist(pt_real, loc_SA_KNNHC); //S_single PointF loc_S_single = fdb.MatchLocation(this.current_fingerprint[Fingerprint_Name.S], FingerprintDataBase.Match_Strategy.Single, null); sum_errs[3] += new MathUtils.MathUtility().point_dist(pt_real, loc_S_single); //S_KNN PointF loc_S_KNN = fdb.MatchLocation(this.current_fingerprint[Fingerprint_Name.S], FingerprintDataBase.Match_Strategy.KNN, new double[] { KNN_K }); sum_errs[4] += new MathUtils.MathUtility().point_dist(pt_real, loc_S_KNN); //S_KNNHC PointF loc_S_KNNHC = fdb.MatchLocation(this.current_fingerprint[Fingerprint_Name.S], FingerprintDataBase.Match_Strategy.KNN_HC, new double[] { KNNHC_K }); sum_errs[5] += new MathUtils.MathUtility().point_dist(pt_real, loc_S_KNNHC); bgAutoTest.ReportProgress((int)((n * avg_cnt + i + 1) / (double)(avg_cnt * snrs.Length) * 100)); System.Threading.Thread.Sleep(200); } for (int m = 0; m < sum_errs.Length; m++ ) { double err = sum_errs[m] / avg_cnt; byte[] tmp = Encoding.ASCII.GetBytes(err.ToString("0.00") + "\t"); data_fs.Write(tmp, 0, tmp.Length); } byte[] rn = Encoding.ASCII.GetBytes("\r\n"); data_fs.Write(rn, 0, rn.Length); } data_fs.Close(); e.Result = true; }
/// <summary> /// 运行单次仿真 /// </summary> public Dictionary<Fingerprint_Name, Fingerprint> RunSingleSimulation(int num_rays, Transmitter.EMIT_OPTION emit_option, Signal_Type signal_type, Noise_Type noise_type, double[] noise_params, int N) { foreach(Transmitter trans in this.Transmitters) { trans.Reset(); trans.EmitRays(num_rays, emit_option); //各发射机辐射波束 foreach(Receiver rec in this.Receivers) { rec.Reset(); if(this.IsLOS(trans.Location, rec.Location)) //若存在LOS情况 { LineSegment segLOS = new LineSegment(trans.Location, rec.Location); Ray rayLOS = new Ray(trans, new LineSegment[] { segLOS }, segLOS.DirectionRadian, trans.CenterFrequency, trans.EmitPower * trans.Gain, trans.EmitPower * trans.Gain, true); rec.AcceptRay(rayLOS, this); } } foreach(Ray ray in trans.Rays) //更新每条ray, 若被接收,则更新接收它的接收机 { Receiver rec = null; while (!ray.Update(this, out rec)) ; if (rec != null) rec.AcceptRay(ray, this); } } FingerprintGenerator fg = new FingerprintGenerator(); Dictionary<Fingerprint_Name, Fingerprint> result = new Dictionary<Fingerprint_Name,Fingerprint>(); List<double>[] ray_angles = new List<double>[this.receivers.Count]; List<double>[] ray_strengths = new List<double>[this.receivers.Count]; List<double>[] ray_taus = new List<double>[this.receivers.Count]; int i = 0; foreach(Receiver rec in this.Receivers) { ray_angles[i] = new List<double>(); ray_strengths[i] = new List<double>(); ray_taus[i] = new List<double>(); foreach(Ray ray in rec.Rays) { ray_angles[i].Add(ray.CurrentDirection); ray_strengths[i].Add(ray.FadedPower(this.MeterPixelRatio)); ray_taus[i].Add(ray.GetLifeSpan(this.MeterPixelRatio)); } i++; } double[] args = null; switch (signal_type) { case Signal_Type.PSEUDO_DOPPLER: if (this.receivers.First().Value.Antenna.AntType != AntennaType.PSEUDO_DOPPLER) return null; Antenna_PseudoDoppler ant = this.receivers.First().Value.Antenna as Antenna_PseudoDoppler; args = fg.PackArgs_PseudoDoppler(this.receivers.First().Value.FrequencyLow, this.receivers.First().Value.CenterFrequency, this.receivers.First().Value.SampleFrequency, ant.SwitchFrequency, ant.Radius, ant.AntennaNumber); break; } for (int j = 0; j < Enum.GetNames(new Fingerprint_Name().GetType()).Length; j++ ) result[(Fingerprint_Name)j] = fg.SingleFingerprintSim(signal_type, noise_type, noise_params, (Fingerprint_Name)j, ray_angles, ray_strengths, ray_taus, N, args); return result; }