Changeset 2177
- Timestamp:
- 05/20/11 18:53:58 (14 years ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/python/scantable.py
r2150 r2177 1083 1083 """\ 1084 1084 Flag the data in 'lag' space by providing a frequency to remove. 1085 Flagged data in the scantable get sinterpolated over the region.1085 Flagged data in the scantable get interpolated over the region. 1086 1086 No taper is applied. 1087 1087 … … 1118 1118 else: 1119 1119 return s 1120 1121 @asaplog_post_dec 1122 def fft(self, getrealimag=False, rowno=[]): 1123 """\ 1124 Apply FFT to the spectra. 1125 Flagged data in the scantable get interpolated over the region. 1126 1127 Parameters: 1128 1129 getrealimag: If True, returns the real and imaginary part 1130 values of the complex results. 1131 If False (the default), returns the amplitude 1132 (absolute value) normalised with Ndata/2 and 1133 phase (argument, in unit of radian). 1134 1135 rowno: The row number(s) to be processed. int, list 1136 and tuple are accepted. By default ([]), FFT 1137 is applied to the whole data. 1138 1139 Returns: 1140 1141 A dictionary containing two keys, i.e., 'real' and 'imag' for 1142 getrealimag = True, or 'ampl' and 'phase' for getrealimag = False, 1143 respectively. 1144 The value for each key is a list of lists containing the FFT 1145 results from each spectrum. 1146 1147 """ 1148 if getrealimag: 1149 res = {"real":[], "imag":[]} # return real and imaginary values 1150 else: 1151 res = {"ampl":[], "phase":[]} # return amplitude and phase(argument) 1152 1153 if isinstance(rowno, int): 1154 rowno = [rowno] 1155 elif not (isinstance(rowno, list) or isinstance(rowno, tuple)): 1156 raise TypeError("The row number(s) should be int, list or tuple.") 1157 1158 nrow = len(rowno) 1159 if nrow == 0: nrow = self.nrow() # by default, calculate for all rows. 1160 1161 fspec = self._math._fft(self, rowno, getrealimag) 1162 nspec = len(fspec)/nrow 1163 1164 i = 0 1165 while (i < nrow): 1166 v1 = [] 1167 v2 = [] 1168 1169 j = 0 1170 while (j < nspec): 1171 k = i*nspec + j 1172 v1.append(fspec[k]) 1173 v2.append(fspec[k+1]) 1174 j += 2 1175 1176 if getrealimag: 1177 res["real"].append(v1) 1178 res["imag"].append(v2) 1179 else: 1180 res["ampl"].append(v1) 1181 res["phase"].append(v2) 1182 1183 i += 1 1184 1185 return res 1120 1186 1121 1187 @asaplog_post_dec -
trunk/src/STMath.cpp
r2163 r2177 11 11 // 12 12 13 #include <sstream> 14 13 15 #include <casa/iomanip.h> 14 #include <casa/Exceptions/Error.h>15 #include <casa/Containers/Block.h>16 #include <casa/BasicSL/String.h>17 16 #include <casa/Arrays/MaskArrLogi.h> 18 17 #include <casa/Arrays/MaskArrMath.h> … … 21 20 #include <casa/Arrays/Slice.h> 22 21 #include <casa/Arrays/Slicer.h> 22 #include <casa/BasicSL/String.h> 23 #include <casa/Containers/Block.h> 23 24 #include <casa/Containers/RecordField.h> 25 #include <casa/Exceptions/Error.h> 26 #include <casa/Logging/LogIO.h> 27 28 #include <coordinates/Coordinates/CoordinateSystem.h> 29 #include <coordinates/Coordinates/CoordinateUtil.h> 30 #include <coordinates/Coordinates/FrequencyAligner.h> 31 #include <coordinates/Coordinates/SpectralCoordinate.h> 32 33 #include <lattices/Lattices/LatticeUtilities.h> 34 35 #include <scimath/Functionals/Polynomial.h> 36 #include <scimath/Mathematics/Convolver.h> 37 #include <scimath/Mathematics/VectorKernel.h> 38 39 #include <tables/Tables/ExprNode.h> 40 #include <tables/Tables/ReadAsciiTable.h> 41 #include <tables/Tables/TableCopy.h> 42 #include <tables/Tables/TableIter.h> 43 #include <tables/Tables/TableParse.h> 44 #include <tables/Tables/TableRecord.h> 24 45 #include <tables/Tables/TableRow.h> 25 46 #include <tables/Tables/TableVector.h> 26 47 #include <tables/Tables/TabVecMath.h> 27 #include <tables/Tables/ExprNode.h>28 #include <tables/Tables/TableRecord.h>29 #include <tables/Tables/TableParse.h>30 #include <tables/Tables/ReadAsciiTable.h>31 #include <tables/Tables/TableIter.h>32 #include <tables/Tables/TableCopy.h>33 #include <scimath/Mathematics/FFTServer.h>34 35 #include <lattices/Lattices/LatticeUtilities.h>36 37 #include <coordinates/Coordinates/SpectralCoordinate.h>38 #include <coordinates/Coordinates/CoordinateSystem.h>39 #include <coordinates/Coordinates/CoordinateUtil.h>40 #include <coordinates/Coordinates/FrequencyAligner.h>41 42 #include <scimath/Mathematics/VectorKernel.h>43 #include <scimath/Mathematics/Convolver.h>44 #include <scimath/Functionals/Polynomial.h>45 48 46 49 #include <atnf/PKSIO/SrcType.h> 47 48 #include <casa/Logging/LogIO.h>49 #include <sstream>50 50 51 51 #include "MathUtils.h" 52 52 #include "RowAccumulator.h" 53 53 #include "STAttr.h" 54 #include "STMath.h" 54 55 #include "STSelector.h" 55 56 56 #include "STMath.h"57 57 using namespace casa; 58 59 58 using namespace asap; 60 59 … … 2821 2820 } 2822 2821 2823 CountedPtr< Scantable > 2824 asap::STMath::lagFlag( const CountedPtr< Scantable > & in, 2822 std::vector<float> 2823 asap::STMath::fft( const casa::CountedPtr< Scantable > & in, 2824 const std::vector<int>& whichrow, 2825 bool getRealImag ) 2826 { 2827 std::vector<float> res; 2828 Table tab = in->table(); 2829 ArrayColumn<Float> specCol(tab, "SPECTRA"); 2830 ArrayColumn<uChar> flagCol(tab, "FLAGTRA"); 2831 FFTServer<Float,Complex> ffts; 2832 2833 if (whichrow.size() < 1) { // for all rows (by default) 2834 int nrow = int(tab.nrow()); 2835 for (int i = 0; i < nrow; ++i) { 2836 Vector<Float> spec = specCol(i); 2837 Vector<uChar> flag = flagCol(i); 2838 doFFT(res, ffts, getRealImag, spec, flag); 2839 } 2840 } else { // for specified rows 2841 for (uInt i = 0; i < whichrow.size(); ++i) { 2842 Vector<Float> spec = specCol(whichrow[i]); 2843 Vector<uChar> flag = flagCol(whichrow[i]); 2844 doFFT(res, ffts, getRealImag, spec, flag); 2845 } 2846 } 2847 2848 return res; 2849 } 2850 2851 void asap::STMath::doFFT( std::vector<float>& out, 2852 FFTServer<Float,Complex>& ffts, 2853 bool getRealImag, 2854 Vector<Float>& spec, 2855 Vector<uChar>& flag ) 2856 { 2857 doZeroOrderInterpolation(spec, flag); 2858 2859 Vector<Complex> fftout; 2860 ffts.fft0(fftout, spec); 2861 2862 float norm = float(2.0/double(spec.size())); 2863 if (getRealImag) { 2864 for (uInt j = 0; j < fftout.size(); ++j) { 2865 out.push_back(real(fftout[j])*norm); 2866 out.push_back(imag(fftout[j])*norm); 2867 } 2868 } else { 2869 for (uInt j = 0; j < fftout.size(); ++j) { 2870 out.push_back(abs(fftout[j])*norm); 2871 out.push_back(arg(fftout[j])); 2872 } 2873 } 2874 2875 } 2876 2877 void asap::STMath::doZeroOrderInterpolation( Vector<Float>& spec, 2878 Vector<uChar>& flag ) 2879 { 2880 int fstart = -1; 2881 int fend = -1; 2882 for (uInt i = 0; i < flag.nelements(); ++i) { 2883 if (flag[i] > 0) { 2884 fstart = i; 2885 while (flag[i] > 0 && i < flag.nelements()) { 2886 fend = i; 2887 i++; 2888 } 2889 } 2890 2891 // execute interpolation as the following criteria: 2892 // (1) for a masked region inside the spectrum, replace the spectral 2893 // values with the mean of those at the two channels just outside 2894 // the both edges of the masked region. 2895 // (2) for a masked region at the spectral edge, replace the values 2896 // with the one at the nearest non-masked channel. 2897 // (ZOH, but bilateral) 2898 Float interp = 0.0; 2899 if (fstart-1 > 0) { 2900 interp = spec[fstart-1]; 2901 if (fend+1 < Int(spec.nelements())) { 2902 interp = (interp + spec[fend+1]) / 2.0; 2903 } 2904 } else { 2905 interp = spec[fend+1]; 2906 } 2907 if (fstart > -1 && fend > -1) { 2908 for (int j = fstart; j <= fend; ++j) { 2909 spec[j] = interp; 2910 } 2911 } 2912 2913 fstart = -1; 2914 fend = -1; 2915 } 2916 } 2917 2918 CountedPtr<Scantable> 2919 asap::STMath::lagFlag( const CountedPtr<Scantable>& in, 2825 2920 double start, double end, 2826 const std::string& mode )2827 { 2828 CountedPtr< Scantable> out = getScantable(in, false);2921 const std::string& mode ) 2922 { 2923 CountedPtr<Scantable> out = getScantable(in, false); 2829 2924 Table& tout = out->table(); 2830 2925 TableIterator iter(tout, "FREQ_ID"); 2831 2926 FFTServer<Float,Complex> ffts; 2927 2832 2928 while ( !iter.pastEnd() ) { 2833 2929 Table tab = iter.table(); … … 2839 2935 ArrayColumn<Float> specCol(tab, "SPECTRA"); 2840 2936 ArrayColumn<uChar> flagCol(tab, "FLAGTRA"); 2937 2841 2938 for (int i=0; i<int(tab.nrow()); ++i) { 2842 2939 Vector<Float> spec = specCol(i); 2843 2940 Vector<uChar> flag = flagCol(i); 2844 int fstart = -1; 2845 int fend = -1; 2846 for (unsigned int k=0; k < flag.nelements(); ++k ) { 2847 if (flag[k] > 0) { 2848 fstart = k; 2849 while (flag[k] > 0 && k < flag.nelements()) { 2850 fend = k; 2851 k++; 2852 } 2853 } 2854 Float interp = 0.0; 2855 if (fstart-1 > 0 ) { 2856 interp = spec[fstart-1]; 2857 if (fend+1 < Int(spec.nelements())) { 2858 interp = (interp+spec[fend+1])/2.0; 2859 } 2860 } else { 2861 interp = spec[fend+1]; 2862 } 2863 if (fstart > -1 && fend > -1) { 2864 for (int j=fstart;j<=fend;++j) { 2865 spec[j] = interp; 2866 } 2867 } 2868 fstart =-1; 2869 fend = -1; 2870 } 2941 doZeroOrderInterpolation(spec, flag); 2942 2871 2943 Vector<Complex> lags; 2872 2944 ffts.fft0(lags, spec); 2945 2873 2946 Int lag0(start+0.5); 2874 2947 Int lag1(end+0.5); … … 2878 2951 } 2879 2952 Int lstart = max(0, lag0); 2880 Int lend = min(Int(lags.nelements()-1), lag1);2953 Int lend = min(Int(lags.nelements()-1), lag1); 2881 2954 if (lstart == lend) { 2882 2955 lags[lstart] = Complex(0.0); … … 2891 2964 } 2892 2965 } 2966 2893 2967 ffts.fft0(spec, lags); 2968 2894 2969 specCol.put(i, spec); 2895 2970 } -
trunk/src/STMath.h
r1937 r2177 13 13 #define ASAPSTMATH_H 14 14 15 #include <map> 15 16 #include <string> 16 #include <map>17 17 18 18 #include <casa/aips.h> 19 #include <casa/Arrays/Vector.h> 20 #include <casa/BasicSL/String.h> 19 21 #include <casa/Utilities/CountedPtr.h> 20 #include <casa/BasicSL/String.h> 21 #include < casa/Arrays/Vector.h>22 23 #include <scimath/Mathematics/FFTServer.h> 22 24 #include <scimath/Mathematics/InterpolateArray1D.h> 23 25 26 #include "Logger.h" 24 27 #include "Scantable.h" 25 28 #include "STDefs.h" 26 29 #include "STPol.h" 27 #include "Logger.h"28 30 29 31 namespace asap { … … 345 347 lagFlag( const casa::CountedPtr<Scantable>& in, double start, 346 348 double end, const std::string& mode="frequency"); 349 350 std::vector<float> 351 fft( const casa::CountedPtr<Scantable>& in, 352 const std::vector<int>& whichrow, 353 bool getRealImag=false ); 347 354 348 355 // test for average spectra with different channel/resolution … … 426 433 int index ) ; 427 434 double getMJD( string strtime ) ; 435 void doFFT( std::vector<float>& out, 436 casa::FFTServer< casa::Float, casa::Complex >& ffts, 437 bool getRealImag, 438 casa::Vector<casa::Float>& spec, 439 casa::Vector<casa::uChar>& flag ) ; 440 void doZeroOrderInterpolation( casa::Vector<casa::Float>& spec, 441 casa::Vector<casa::uChar>& flag) ; 428 442 429 443 bool insitu_; -
trunk/src/STMathWrapper.h
r1907 r2177 213 213 mode)); } 214 214 215 std::vector<float> fft( const ScantableWrapper& in, 216 const std::vector<int>& whichrow, 217 const bool getRealImag=false ) 218 { return STMath::fft(in.getCP(), whichrow, getRealImag); } 219 215 220 // test for average spectra with different channel/resolution 216 221 ScantableWrapper -
trunk/src/python_STMath.cpp
r1907 r2177 77 77 .def("_mx_extract", &STMathWrapper::mxExtract) 78 78 .def("_lag_flag", &STMathWrapper::lagFlag) 79 .def("_fft", &STMathWrapper::fft) 79 80 // testing average spectra with different channel/resolution 80 81 .def("_new_average", &STMathWrapper::new_average)
Note:
See TracChangeset
for help on using the changeset viewer.