mirror of
				https://github.com/gnss-sdr/gnss-sdr
				synced 2025-10-30 23:03:05 +00:00 
			
		
		
		
	Use boost circular buffers
This commit is contained in:
		| @@ -33,206 +33,103 @@ | ||||
|  | ||||
| #ifndef GNSS_SDR_CIRCULAR_DEQUE_H_ | ||||
| #define GNSS_SDR_CIRCULAR_DEQUE_H_ | ||||
| #include <exception> | ||||
| #include <vector> | ||||
| #include <boost/circular_buffer.hpp> | ||||
|  | ||||
| template <class T> | ||||
| class Gnss_circular_deque | ||||
| { | ||||
| public: | ||||
|     Gnss_circular_deque(); | ||||
|     Gnss_circular_deque(const unsigned int max_size, const unsigned int nchann); | ||||
|     ~Gnss_circular_deque(); | ||||
|     unsigned int size(const unsigned int ch); | ||||
|     T& at(const unsigned int ch, const unsigned int pos); | ||||
|     T& front(const unsigned int ch); | ||||
|     T& back(const unsigned int ch); | ||||
|     void push_back(const unsigned int ch, const T& new_data); | ||||
|     T pop_front(const unsigned int ch); | ||||
|     void clear(const unsigned int ch); | ||||
|     T* get_vector(const unsigned int ch); | ||||
|     Gnss_circular_deque();                                                        // Default constructor | ||||
|     Gnss_circular_deque(const unsigned int max_size, const unsigned int nchann);  // nchann = number of channels; max_size = channel capacity | ||||
|     unsigned int size(const unsigned int ch);                                     // Returns the number of available elements in a channel | ||||
|     T& at(const unsigned int ch, const unsigned int pos);                         // Returns a reference to an element | ||||
|     T& front(const unsigned int ch);                                              // Returns a reference to the first element in the deque | ||||
|     T& back(const unsigned int ch);                                               // Returns a reference to the last element in the deque | ||||
|     void push_back(const unsigned int ch, const T& new_data);                     // Inserts an element at the end of the deque | ||||
|     void pop_front(const unsigned int ch);                                        // Removes the first element of the deque | ||||
|     void clear(const unsigned int ch);                                            // Removes all the elements of the deque (Sets size to 0). Capacity is not modified | ||||
|     void reset(const unsigned int max_size, const unsigned int nchann);           // Removes all the elements in all the channels. Re-sets the number of channels and their capacity | ||||
|     void reset();                                                                 // Removes all the channels (Sets nchann to 0) | ||||
|  | ||||
| private: | ||||
|     T** d_history; | ||||
|     T d_return_void;  // Void object for avoid compiler errors | ||||
|     unsigned int* d_index_pop; | ||||
|     unsigned int* d_index_push; | ||||
|     unsigned int* d_size; | ||||
|     unsigned int d_max_size; | ||||
|     unsigned int d_nchannels; | ||||
|     std::vector<boost::circular_buffer<T>> d_data; | ||||
| }; | ||||
|  | ||||
|  | ||||
| template <class T> | ||||
| Gnss_circular_deque<T>::Gnss_circular_deque() | ||||
| { | ||||
|     d_max_size = 0; | ||||
|     d_nchannels = 0; | ||||
|     d_size = nullptr; | ||||
|     d_index_pop = nullptr; | ||||
|     d_index_push = nullptr; | ||||
|     d_history = nullptr; | ||||
|     reset(); | ||||
| } | ||||
|  | ||||
| template <class T> | ||||
| Gnss_circular_deque<T>::Gnss_circular_deque(const unsigned int max_size, const unsigned int nchann) | ||||
| { | ||||
|     d_max_size = max_size; | ||||
|     d_nchannels = nchann; | ||||
|     if (d_max_size > 0 and d_nchannels > 0) | ||||
|         { | ||||
|             d_size = new unsigned int[d_nchannels]; | ||||
|             d_index_pop = new unsigned int[d_nchannels]; | ||||
|             d_index_push = new unsigned int[d_nchannels]; | ||||
|             d_history = new T*[d_nchannels]; | ||||
|             for (unsigned int i = 0; i < d_nchannels; i++) | ||||
|                 { | ||||
|                     d_size[i] = 0; | ||||
|                     d_index_pop[i] = 0; | ||||
|                     d_index_push[i] = 0; | ||||
|                     d_history[i] = new T[d_max_size]; | ||||
|                     for (unsigned int ii = 0; ii < d_max_size; ii++) | ||||
|                         { | ||||
|                             d_history[i][ii] = d_return_void; | ||||
|                         } | ||||
|                 } | ||||
|         } | ||||
| } | ||||
|  | ||||
| template <class T> | ||||
| Gnss_circular_deque<T>::~Gnss_circular_deque() | ||||
| { | ||||
|     if (d_max_size > 0 and d_nchannels > 0) | ||||
|         { | ||||
|             delete[] d_size; | ||||
|             delete[] d_index_pop; | ||||
|             delete[] d_index_push; | ||||
|             for (unsigned int i = 0; i < d_nchannels; i++) | ||||
|                 { | ||||
|                     delete[] d_history[i]; | ||||
|                 } | ||||
|             delete[] d_history; | ||||
|         } | ||||
|     reset(max_size, nchann); | ||||
| } | ||||
|  | ||||
| template <class T> | ||||
| unsigned int Gnss_circular_deque<T>::size(const unsigned int ch) | ||||
| { | ||||
|     return d_size[ch]; | ||||
|     return d_data.at(ch).size(); | ||||
| } | ||||
|  | ||||
| template <class T> | ||||
| T& Gnss_circular_deque<T>::back(const unsigned int ch) | ||||
| { | ||||
|     if (d_size[ch] > 0) | ||||
|         { | ||||
|             unsigned int index = 0; | ||||
|             if (d_index_push[ch] > 0) | ||||
|                 { | ||||
|                     index = d_index_push[ch] - 1; | ||||
|                 } | ||||
|             else | ||||
|                 { | ||||
|                     index = d_max_size - 1; | ||||
|                 } | ||||
|             return d_history[ch][index]; | ||||
|         } | ||||
|     else | ||||
|         { | ||||
|             std::exception ex; | ||||
|             throw ex; | ||||
|             return d_return_void; | ||||
|         } | ||||
|     return d_data.at(ch).back(); | ||||
| } | ||||
|  | ||||
|  | ||||
| template <class T> | ||||
| T& Gnss_circular_deque<T>::front(const unsigned int ch) | ||||
| { | ||||
|     if (d_size[ch] > 0) | ||||
|         { | ||||
|             return d_history[ch][d_index_pop[ch]]; | ||||
|         } | ||||
|     else | ||||
|         { | ||||
|             std::exception ex; | ||||
|             throw ex; | ||||
|             return d_return_void; | ||||
|         } | ||||
|     return d_data.at(ch).front(); | ||||
| } | ||||
|  | ||||
|  | ||||
| template <class T> | ||||
| T& Gnss_circular_deque<T>::at(const unsigned int ch, const unsigned int pos) | ||||
| { | ||||
|     if (d_size[ch] > 0 and pos < d_size[ch]) | ||||
|         { | ||||
|             unsigned int index = ((d_index_pop[ch] + pos) % d_max_size); | ||||
|             return d_history[ch][index]; | ||||
|         } | ||||
|     else | ||||
|         { | ||||
|             std::exception ex; | ||||
|             throw ex; | ||||
|             return d_return_void; | ||||
|         } | ||||
|     return d_data.at(ch).at(pos); | ||||
| } | ||||
|  | ||||
| template <class T> | ||||
| void Gnss_circular_deque<T>::clear(const unsigned int ch) | ||||
| { | ||||
|     d_size[ch] = 0; | ||||
|     d_index_pop[ch] = 0; | ||||
|     d_index_push[ch] = 0; | ||||
|     d_data.at(ch).clear(); | ||||
| } | ||||
|  | ||||
| template <class T> | ||||
| T Gnss_circular_deque<T>::pop_front(const unsigned int ch) | ||||
| void Gnss_circular_deque<T>::reset(const unsigned int max_size, const unsigned int nchann) | ||||
| { | ||||
|     if (d_size[ch] > 0) | ||||
|     d_data.clear(); | ||||
|     if (max_size > 0 and nchann > 0) | ||||
|         { | ||||
|             d_size[ch]--; | ||||
|             T result = d_history[ch][d_index_pop[ch]]; | ||||
|             if (d_size[ch] > 0) | ||||
|             for (unsigned int i = 0; i < nchann; i++) | ||||
|                 { | ||||
|                     d_index_pop[ch]++; | ||||
|                     d_index_pop[ch] %= d_max_size; | ||||
|                     d_data.push_back(boost::circular_buffer<T>(max_size)); | ||||
|                 } | ||||
|             else | ||||
|                 { | ||||
|                     clear(ch); | ||||
|                 } | ||||
|             return result; | ||||
|         } | ||||
|     else | ||||
|         { | ||||
|             std::exception ex; | ||||
|             throw ex; | ||||
|             return d_return_void; | ||||
|         } | ||||
| } | ||||
|  | ||||
| template <class T> | ||||
| void Gnss_circular_deque<T>::reset() | ||||
| { | ||||
|     d_data.clear(); | ||||
| } | ||||
|  | ||||
| template <class T> | ||||
| void Gnss_circular_deque<T>::pop_front(const unsigned int ch) | ||||
| { | ||||
|     d_data.at(ch).pop_front(); | ||||
| } | ||||
|  | ||||
| template <class T> | ||||
| void Gnss_circular_deque<T>::push_back(const unsigned int ch, const T& new_data) | ||||
| { | ||||
|     bool increment_pop = true; | ||||
|     if (d_size[ch] < d_max_size) | ||||
|         { | ||||
|             increment_pop = false; | ||||
|             d_size[ch]++; | ||||
|         } | ||||
|  | ||||
|     d_history[ch][d_index_push[ch]] = new_data; | ||||
|     d_index_push[ch]++; | ||||
|     d_index_push[ch] %= d_max_size; | ||||
|     if (increment_pop) | ||||
|         { | ||||
|             d_index_pop[ch] = d_index_push[ch]; | ||||
|         } | ||||
|     d_data.at(ch).push_back(new_data); | ||||
| } | ||||
|  | ||||
| template <class T> | ||||
| T* Gnss_circular_deque<T>::get_vector(const unsigned int ch) | ||||
| { | ||||
|     return d_history[ch]; | ||||
| } | ||||
|  | ||||
|  | ||||
| #endif /* GNSS_SDR_CIRCULAR_DEQUE_H_ */ | ||||
|   | ||||
| @@ -66,7 +66,7 @@ hybrid_observables_cc::hybrid_observables_cc(unsigned int nchannels_in, | ||||
|     max_delta = 0.15;     // 150 ms | ||||
|     valid_channels.resize(d_nchannels, false); | ||||
|     d_num_valid_channels = 0; | ||||
|     d_gnss_synchro_history = new Gnss_circular_deque<Gnss_Synchro>(200, d_nchannels); | ||||
|     d_gnss_synchro_history = new Gnss_circular_deque<Gnss_Synchro>(1000, d_nchannels); | ||||
|  | ||||
|     // ############# ENABLE DATA FILE LOG ################# | ||||
|     if (d_dump) | ||||
| @@ -307,25 +307,19 @@ bool hybrid_observables_cc::interpolate_data(Gnss_Synchro &out, const unsigned i | ||||
|         } | ||||
|     std::pair<unsigned int, unsigned int> ind = find_interp_elements(ch, ti); | ||||
|  | ||||
|     //Linear interpolation parameters: y(t) = m * t + c | ||||
|     double m = 0.0; | ||||
|     double c = 0.0; | ||||
|     //Linear interpolation: y(t) = y(t1) + (y(t2) - y(t1)) * (t - t1) / (t2 - t1) | ||||
|  | ||||
|     // CARRIER PHASE INTERPOLATION | ||||
|  | ||||
|     m = (d_gnss_synchro_history->at(ch, ind.first).Carrier_phase_rads - d_gnss_synchro_history->at(ch, ind.second).Carrier_phase_rads) / (d_gnss_synchro_history->at(ch, ind.first).RX_time - d_gnss_synchro_history->at(ch, ind.second).RX_time); | ||||
|     c = d_gnss_synchro_history->at(ch, ind.first).Carrier_phase_rads - m * d_gnss_synchro_history->at(ch, ind.first).RX_time; | ||||
|     out.Carrier_phase_rads = m * ti + c; | ||||
|     out.Carrier_phase_rads = d_gnss_synchro_history->at(ch, ind.first).Carrier_phase_rads + (d_gnss_synchro_history->at(ch, ind.second).Carrier_phase_rads - d_gnss_synchro_history->at(ch, ind.first).Carrier_phase_rads) * (ti - d_gnss_synchro_history->at(ch, ind.first).RX_time) / (d_gnss_synchro_history->at(ch, ind.second).RX_time - d_gnss_synchro_history->at(ch, ind.first).RX_time); | ||||
|  | ||||
|     // CARRIER DOPPLER INTERPOLATION | ||||
|     m = (d_gnss_synchro_history->at(ch, ind.first).Carrier_Doppler_hz - d_gnss_synchro_history->at(ch, ind.second).Carrier_Doppler_hz) / (d_gnss_synchro_history->at(ch, ind.first).RX_time - d_gnss_synchro_history->at(ch, ind.second).RX_time); | ||||
|     c = d_gnss_synchro_history->at(ch, ind.first).Carrier_Doppler_hz - m * d_gnss_synchro_history->at(ch, ind.first).RX_time; | ||||
|     out.Carrier_Doppler_hz = m * ti + c; | ||||
|  | ||||
|     out.Carrier_Doppler_hz = d_gnss_synchro_history->at(ch, ind.first).Carrier_Doppler_hz + (d_gnss_synchro_history->at(ch, ind.second).Carrier_Doppler_hz - d_gnss_synchro_history->at(ch, ind.first).Carrier_Doppler_hz) * (ti - d_gnss_synchro_history->at(ch, ind.first).RX_time) / (d_gnss_synchro_history->at(ch, ind.second).RX_time - d_gnss_synchro_history->at(ch, ind.first).RX_time); | ||||
|  | ||||
|     // TOW INTERPOLATION | ||||
|     m = (d_gnss_synchro_history->at(ch, ind.first).TOW_at_current_symbol_s - d_gnss_synchro_history->at(ch, ind.second).TOW_at_current_symbol_s) / (d_gnss_synchro_history->at(ch, ind.first).RX_time - d_gnss_synchro_history->at(ch, ind.second).RX_time); | ||||
|     c = d_gnss_synchro_history->at(ch, ind.first).TOW_at_current_symbol_s - m * d_gnss_synchro_history->at(ch, ind.first).RX_time; | ||||
|     out.TOW_at_current_symbol_s = m * ti + c; | ||||
|  | ||||
|     out.TOW_at_current_symbol_s = d_gnss_synchro_history->at(ch, ind.first).TOW_at_current_symbol_s + (d_gnss_synchro_history->at(ch, ind.second).TOW_at_current_symbol_s - d_gnss_synchro_history->at(ch, ind.first).TOW_at_current_symbol_s) * (ti - d_gnss_synchro_history->at(ch, ind.first).RX_time) / (d_gnss_synchro_history->at(ch, ind.second).RX_time - d_gnss_synchro_history->at(ch, ind.first).RX_time); | ||||
|  | ||||
|     return true; | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Antonio Ramos
					Antonio Ramos