public static void stop_execution(niFgen niFgenObj, niScope niScopeObj) { niFgenObj.AbortGeneration(); Debug.WriteLine("[NIFGEN] Generation aborted."); niFgenObj.ClearArbMemory(); Debug.WriteLine("[NIFGEN] Memory cleared."); niFgenObj.Dispose(); Debug.WriteLine("[NIFGEN] Successfully disposed."); niScopeObj.Abort(); Debug.WriteLine("[NISCOPE] Acquisition aborted."); niScopeObj.Dispose(); Debug.WriteLine("[NISCOPE] Successfully disposed."); }
public void RXThread() { // Acquisition parameters string NISCOPE_resource_name = "PXI1Slot5"; string NISCOPE_channel_name = (string)"1"; int NISCOPE_num_waveform = 1; double NISCOPE_sample_rate = fs_rx; // Configure vertical properties of the acquisition int NISCOPE_coupling = niScopeConstants.Dc; double NISCOPE_vertical_range = 3; //%1;% 5M %%1.3M: % without amp 0.4;% with amplifier 0.5; double NISCOPE_vertical_offset = 0; int NISCOPE_probe_attenuation = 1; // %%1.3M % without amplifier 10;% with amplifier 1; int NISCOPE_impedance = 50; // Horizontal properties of the acquisition int NISCOPE_min_record_length = 1000; // default = 1000 double NISCOPE_ref_position = 0; // default = 50% // Trigger properties double NISCOPE_trigger_holdoff = 0; double NISCOPE_trigger_delay = 0; // Fetch parameters int NISCOPE_max_per_fetch; double NISCOPE_timeout = 0; short[] waveform16; // double[] waveformD; // short[] niscope_data; // short[] niscope_all_data; // double[] niscope_data; niScopeWfmInfo[] waveform_info = new niScopeWfmInfo[NISCOPE_num_waveform]; int cnt_acq = 1; niScopeObj = new niScope(NISCOPE_resource_name, true, true); niScopeObj.AutoSetup(); Debug.WriteLine("[NISCOPE] Auto setup"); niScopeObj.ConfigureVertical(NISCOPE_channel_name, NISCOPE_vertical_range, NISCOPE_vertical_offset, NISCOPE_coupling, NISCOPE_probe_attenuation, true); Debug.WriteLine("[NISCOPE] Configured vertical"); niScopeObj.ConfigureChanCharacteristics(NISCOPE_channel_name, NISCOPE_impedance, NISCOPE_sample_rate); Debug.WriteLine("[NISCOPE] Configured channel char"); niScopeObj.ConfigureHorizontalTiming(NISCOPE_sample_rate, NISCOPE_min_record_length, NISCOPE_ref_position, NISCOPE_num_waveform, true); Debug.WriteLine("[NISCOPE] Configured horixontal"); niScopeObj.ConfigureTriggerSoftware(NISCOPE_trigger_holdoff, NISCOPE_trigger_delay); Debug.WriteLine("[NISCOPE] Configured trigger"); niScopeObj.SetInt32(niScopeProperties.FetchRelativeTo, niScopeConstants.ReadPointer); Debug.WriteLine(niScopeObj.GetInt32(niScopeProperties.FetchRelativeTo)); int mem = niScopeObj.GetInt32(niScopeProperties.OnboardMemorySize); Debug.WriteLine("[NISCOPE] Available memory {0}", mem); NISCOPE_max_per_fetch = (int)mem / 16; Debug.WriteLine("[NISCOPE] Max per fetch {0}", NISCOPE_max_per_fetch); // waveform16 = new short[NISCOPE_max_per_fetch]; // waveformD = new double[NISCOPE_max_per_fetch]; // niscope_data = new short[(int)mem / 4]; // niscope_all_data = new short[(int)mem / 4]; // niscope_data = new double[(int)mem / 4]; // Debug.WriteLine("[NISCOPE] Circular buffer length {0}", niscope_data.Length); // int last_write = 0; int last_all_write = 0; var watch2 = System.Diagnostics.Stopwatch.StartNew(); // watch2.Restart(); // while (watch2.ElapsedMilliseconds < 1000) ; // watch2.Stop(); while (!(nifgen_configured)) { ; } try { // Thread.Sleep(400); // while (!(niMT.nifgen_initiated)) ; niScopeObj.InitiateAcquisition(); Debug.WriteLine("[NISCOPE] Initiated scope."); // niscope_initiated = true; var watch_total = System.Diagnostics.Stopwatch.StartNew(); int[] corr_array = { 1, 1, 1, 1, 1, -1, -1, 1, 1, -1, 1, -1, 1 }; double barker_th = 5.0 * NISCOPE_vertical_range / 2.0; int corr_length = corr_array.Length; int n_guard = (int)(fs_rx / fsym_tx); int n_ahead = n_guard; int n_prev = n_ahead + corr_length + 1; short[] x_rec_prev = new short[n_prev]; bool data_started = false; bool data_ended = true; bool data_started_here; int prev_written_count = 0; int total_fetched = 0; int local_fetched; double fetch_gain; double fetch_offset; double fetch_gain_prev = 0; double fetch_offset_prev = 0; double corr_first_old = 0; double corr_end_old = 0; int corr_first_peak_idx = 0; int corr_end_peak_idx = -(n_ahead + 1); double corr_first; double corr_end; int i_end; int rx_data_buffer_length = rx_data_buffer.Length; int ctr = 0; while (ctr < rx_data_buffer_length - 1) { data_started_here = false; // watch2.Restart(); // niScopeObj.Fetch(NISCOPE_channel_name, NISCOPE_timeout, NISCOPE_max_per_fetch, waveformD, waveform_info); waveform16 = new short[NISCOPE_max_per_fetch]; RXwatch.Start(); niScopeObj.FetchBinary16(NISCOPE_channel_name, NISCOPE_timeout, NISCOPE_max_per_fetch, waveform16, waveform_info); // watch2.Stop(); // Debug.WriteLine("[NISCOPE] Fetched {0}", waveform_info[0].ActualSamples); // Debug.WriteLine("[NISCOPE] Fetched waveform #{0}, length {1}, took {2} ticks, gain {3}", cnt_acq, waveform_info[0].ActualSamples, watch2.ElapsedTicks, waveform_info[0].Gain); cnt_acq += 1; try { // watch2.Restart(); // Buffer.BlockCopy(waveformD, 0, niscope_data, last_write, waveform_info[0].ActualSamples * sizeof(double)); // last_write += waveform_info[0].ActualSamples * sizeof(double); // watch2.Stop(); // Debug.WriteLine("[NISCOPE] Wrote to buffer. Took {0} ticks.", watch2.ElapsedTicks); fetch_gain = waveform_info[0].Gain; fetch_offset = waveform_info[0].Offset; local_fetched = waveform_info[0].ActualSamples; for (int i = 0; i < local_fetched; i++) { corr_first = 0; corr_end = 0; i_end = i + n_ahead; for (int j = 0; j < corr_length; j++) { if (data_started) { if (i_end + j < n_prev) { corr_end += ((x_rec_prev[i_end + j] - fetch_offset_prev) * fetch_gain_prev) * corr_array[j]; } else { corr_end += ((waveform16[i_end + j - n_prev] - fetch_offset) * fetch_gain) * corr_array[j]; } } else if (data_ended && (i > (corr_end_peak_idx + n_ahead))) { if (i + j < n_prev) { corr_first += ((x_rec_prev[i + j] - fetch_offset_prev) * fetch_gain_prev) * corr_array[j]; } else { corr_first += ((waveform16[i + j - n_prev] - fetch_offset) * fetch_gain) * corr_array[j]; } } } if (data_started) { if ((corr_end > barker_th)) {/* * // Cross correlation exceeded threshold * if (corr_end > corr_end_old) * { * // Correlation is still increasing * corr_end_old = corr_end; * } * else * {*/ // We reached correlation peak in the previous sample Debug.WriteLine("Data ended {0}", i); data_ended = true; data_started = false; corr_end_old = 0; corr_end_peak_idx = i; // } } } else if (data_ended && (i > (corr_end_peak_idx + n_ahead))) { if ((corr_first > barker_th)) { /*// Cross correlation exceeded threshold * if (corr_first > corr_first_old) * { * // Correlation is still increasing * corr_first_old = corr_first; * } * else * {*/ Debug.WriteLine("Data started {0} threshold {1} corr {2}", i, barker_th, corr_first); // We reached correlation peak in the previous sample corr_first_peak_idx = i; data_started = true; data_started_here = true; data_ended = false; corr_first_old = 0; // } } } if (!data_ended && ((data_started && !data_started_here) || (data_started && data_started_here && (i > corr_first_peak_idx + n_guard + corr_length - 1)))) { if (i < n_prev) { // niscope_data[last_write] = x_rec_prev[i]; rx_data_buffer[rx_data_buffer_last_write] = x_rec_prev[i]; } else { // niscope_data[last_write] = waveform16[i - n_prev]; rx_data_buffer[rx_data_buffer_last_write] = waveform16[i - n_prev]; } rx_data_buffer_last_write += 1; } } Buffer.BlockCopy(waveform16, (local_fetched - n_prev) * sizeof(short), x_rec_prev, 0, n_prev * sizeof(short)); total_fetched += local_fetched; Debug.WriteLine("[NISCOPE] Total fetched {0}, local fetched {1}, exceeding threshold {2} data started? {3} data ended? {4}", total_fetched, local_fetched - corr_length + 1, rx_data_buffer_last_write - prev_written_count, data_started, data_ended); prev_written_count = rx_data_buffer_last_write; ctr = rx_data_buffer_last_write; fetch_gain_prev = fetch_gain; fetch_offset_prev = fetch_offset; // Buffer.BlockCopy(waveform16, 0, niscope_all_data, last_all_write, local_fetched * sizeof(short)); last_all_write += local_fetched * sizeof(short); corr_end_peak_idx = 0; } catch (Exception e) { watch_total.Stop(); Debug.WriteLine("[NISCOPE] Rethrowing exception {0}", e); Debug.WriteLine("[NISCOPE] Last write {0}", rx_data_buffer_last_write); Debug.WriteLine("[NISCOPE] Totalruntime {0}", watch_total.ElapsedTicks); // string file_name = "rx_waveform_fc" + fc.ToString() + "_fsym_tx" + fsym_tx.ToString() + "_fs_tx" + fs_tx.ToString() + "_fs_rx" + fs_rx.ToString() + ".txt"; // int file_ctr = 1; // string new_file_name; // do // { // new_file_name = file_ctr.ToString() + file_name; // file_ctr += 1; // } while (File.Exists(new_file_name)); // // File.WriteAllLines(new_file_name, niscope_data.Select(d => d.ToString())); throw; } // watch2.Restart(); // File.AppendAllLines(@"waveform.txt", waveform.Select(d => d.ToString())); // watch2.Stop(); // Debug.WriteLine("[NISCOPE] Wrote to file. Took {0} ticks.", watch2.ElapsedTicks); } // File.AppendAllText(@"DebugFFMPEG.txt", now.TimeOfDay.ToString() + ffmpegProg + Environment.NewLine); } catch (Exception e) { Debug.WriteLine("[NISCOPE] Exception caught {0}", e); niScopeObj.Abort(); Debug.WriteLine("[NISCOPE] Acquisition aborted."); niScopeObj.Dispose(); Debug.WriteLine("[NISCOPE] Successfully disposed."); niscope_ended = true; // write rx signal /* string file_name = "rx_waveform_fc" + fc.ToString() + "_fsym_tx" + fsym_tx.ToString() + "_fs_tx" + fs_tx.ToString() + "_fs_rx" + fs_rx.ToString() + "_webcam" + (webcam_bool ? 1 : 0).ToString() + "_tx_range" + byte_range.ToString() + ".txt"; * int file_ctr = 1; * string new_file_name; * do * { * new_file_name = file_ctr.ToString() + file_name; * file_ctr += 1; * } while (File.Exists(new_file_name)); * * // File.WriteAllLines(new_file_name, niscope_data.Select(d => d.ToString())); * string file_name2 = "rx_all_waveform_fc" + fc.ToString() + "_fsym_tx" + fsym_tx.ToString() + "_fs_tx" + fs_tx.ToString() + "_fs_rx" + fs_rx.ToString() + "_webcam" + (webcam_bool ? 1 : 0).ToString() + "_tx_range" + byte_range.ToString() + ".txt"; * string new_file_name2 = (file_ctr-1).ToString() + file_name2; * // File.WriteAllLines(new_file_name2, niscope_all_data.Select(d => d.ToString())); * * stop_execution(niFgenObj, niScopeObj); * */ } }