35 #ifndef OPENMS_FILTERING_TRANSFORMERS_LINEARRESAMPLERALIGN_H
36 #define OPENMS_FILTERING_TRANSFORMERS_LINEARRESAMPLERALIGN_H
66 template <
template <
typename>
class SpecT,
typename PeakType>
67 void raster(SpecT<PeakType>& spectrum)
70 if (spectrum.empty())
return;
72 typename SpecT<PeakType>::iterator first = spectrum.begin();
73 typename SpecT<PeakType>::iterator last = spectrum.end();
75 double end_pos = (last - 1)->getMZ();
76 double start_pos = first->getMZ();
77 int number_resampled_points = (int)(ceil((end_pos - start_pos) /
spacing_ + 1));
79 typename std::vector<PeakType> resampled_peak_container;
80 resampled_peak_container.resize(number_resampled_points);
83 typename std::vector<PeakType>::iterator it = resampled_peak_container.begin();
84 for (
int i = 0; i < number_resampled_points; ++i)
90 raster(spectrum.begin(), spectrum.end(), resampled_peak_container.begin(), resampled_peak_container.end());
92 spectrum.swap(resampled_peak_container);
98 template <
template <
typename>
class SpecT,
typename PeakType>
99 void raster_align(SpecT<PeakType>& spectrum,
double start_pos,
double end_pos)
102 if (spectrum.empty())
return;
104 if (end_pos < start_pos)
106 typename std::vector<PeakType> empty;
107 spectrum.swap(empty);
111 typename SpecT<PeakType>::iterator first = spectrum.begin();
112 typename SpecT<PeakType>::iterator last = spectrum.end();
115 while (first != spectrum.end() && (first)->getMZ() < start_pos) {++first; }
116 while (last != first && (last - 1)->getMZ() > end_pos) {--last; }
118 int number_resampled_points = (int)(ceil((end_pos - start_pos) /
spacing_ + 1));
120 typename std::vector<PeakType> resampled_peak_container;
121 resampled_peak_container.resize(number_resampled_points);
124 typename std::vector<PeakType>::iterator it = resampled_peak_container.begin();
125 for (
int i = 0; i < number_resampled_points; ++i)
127 it->setMZ(start_pos + i *
spacing_);
131 raster(first, last, resampled_peak_container.begin(), resampled_peak_container.end());
133 spectrum.swap(resampled_peak_container);
139 template <
typename PeakTypeIterator,
typename ConstPeakTypeIterator>
140 void raster(ConstPeakTypeIterator raw_it, ConstPeakTypeIterator raw_end, PeakTypeIterator resample_it, PeakTypeIterator resample_end)
142 PeakTypeIterator resample_start = resample_it;
145 while (raw_it != raw_end && raw_it->getMZ() < resample_it->getMZ())
147 resample_it->setIntensity(resample_it->getIntensity() + raw_it->getIntensity());
151 while (raw_it != raw_end)
154 while (resample_it != resample_end && resample_it->getMZ() < raw_it->getMZ()) {resample_it++; }
155 if (resample_it != resample_start) {resample_it--; }
158 if ((resample_it + 1) == resample_end) {
break; }
160 double dist_left = fabs(raw_it->getMZ() - resample_it->getMZ());
161 double dist_right = fabs(raw_it->getMZ() - (resample_it + 1)->getMZ());
164 resample_it->setIntensity(resample_it->getIntensity() + raw_it->getIntensity() * dist_right / (dist_left + dist_right));
165 (resample_it + 1)->setIntensity((resample_it + 1)->getIntensity() + raw_it->getIntensity() * dist_left / (dist_left + dist_right));
171 while (raw_it != raw_end)
173 resample_it->setIntensity(resample_it->getIntensity() + raw_it->getIntensity());
181 template <
typename PeakTypeIterator>
182 void raster_interpolate(PeakTypeIterator raw_it, PeakTypeIterator raw_end, PeakTypeIterator it, PeakTypeIterator resampled_end)
184 PeakTypeIterator raw_start = raw_it;
187 while (it != resampled_end && it->getMZ() < raw_it->getMZ()) {it++; }
189 while (it != resampled_end)
192 while (raw_it != raw_end && raw_it->getMZ() < it->getMZ()) {raw_it++; }
193 if (raw_it != raw_start) {raw_it--; }
196 if ((raw_it + 1) == raw_end) {
break; }
199 double m = ((raw_it + 1)->getIntensity() - raw_it->getIntensity()) / ((raw_it + 1)->getMZ() - raw_it->getMZ());
200 it->setIntensity(raw_it->getIntensity() + (it->getMZ() - raw_it->getMZ()) * m);
void raster(SpecT< PeakType > &spectrum)
Applies the resampling algorithm to an MSSpectrum.
Definition: LinearResamplerAlign.h:67
Linear Resampling of raw data.
Definition: LinearResampler.h:61
A 2-dimensional raw data point or peak.
Definition: Peak2D.h:55
void raster_align(SpecT< PeakType > &spectrum, double start_pos, double end_pos)
Applies the resampling algorithm to an MSSpectrum but it will be aligned between start_pos and end_po...
Definition: LinearResamplerAlign.h:99
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
Linear Resampling of raw data with alignment.
Definition: LinearResamplerAlign.h:57
void raster(ConstPeakTypeIterator raw_it, ConstPeakTypeIterator raw_end, PeakTypeIterator resample_it, PeakTypeIterator resample_end)
Applies the resampling algorithm to an MSSpectrum.
Definition: LinearResamplerAlign.h:140
double spacing_
Spacing of the resampled data.
Definition: LinearResampler.h:162
void raster_interpolate(PeakTypeIterator raw_it, PeakTypeIterator raw_end, PeakTypeIterator it, PeakTypeIterator resampled_end)
Applies the resampling algorithm using a linear interpolation.
Definition: LinearResamplerAlign.h:182