// Fill LVector with the basis functions corresponding to each real DOF void LVector::fillBasis(double x, double y, double sigma) { take_ownership(); // fill with psi_pq(z), where psi now defined to have 1/sigma^2 in // front. std::complex<double> z(x,-y); double rsq = norm(z); double tq = std::exp(-0.5*rsq) / (2*M_PI*sigma*sigma); double tqm1=tq; double tqm2; // Ascend m=0 first (*_v)[PQIndex(0,0).rIndex()]=tq; if (_order>=2) { tq = (rsq-1.)*tqm1; (*_v)[PQIndex(1,1).rIndex()] = tq; } PQIndex pq(2,2); for (int p=2; 2*p<=_order; ++p, pq.incN()) { tqm2 = tqm1; tqm1 = tq; tq = ((rsq-2.*p+1.)*tqm1 - (p-1.)*tqm2)/p; (*_v)[pq.rIndex()] = tq; } // Ascend all positive m's std::complex<double> zm = 2* (*_v)[PQIndex(0,0).rIndex()] * z; for (int m=1; m<=_order; m++) { pq.setPQ(m,0); double *r = &(*_v)[pq.rIndex()]; *r = zm.real(); *(r+1) = zm.imag(); tq = 1.; tqm1 = 0.; for (pq.incN(); !pq.pastOrder(_order); pq.incN()) { tqm2 = tqm1; tqm1 = tq; int p=pq.getP(); int q=pq.getQ(); tq = ( (rsq-(p+q-1.))*tqm1 - sqrtn(p-1)*sqrtn(q-1)*tqm2) / (sqrtn(p)*sqrtn(q)); double *r = &(*_v)[pq.rIndex()]; *r = tq*zm.real(); *(r+1) = tq*zm.imag(); } zm *= z/sqrtn(m+1); } }
void UVImager::GetUVPosition(num_t &u, num_t &v, size_t timeIndex, size_t frequencyIndex, TimeFrequencyMetaDataCPtr metaData) { num_t frequency = metaData->Band().channels[frequencyIndex].frequencyHz; u = metaData->UVW()[timeIndex].u * frequency / SpeedOfLight(); v = metaData->UVW()[timeIndex].v * frequency / SpeedOfLight(); return; const Baseline &baseline = metaData->Baseline(); num_t delayDirectionRA = metaData->Field().delayDirectionRA; num_t delayDirectionDec = metaData->Field().delayDirectionDec; double time = metaData->ObservationTimes()[timeIndex]; num_t pointingLattitude = delayDirectionRA; num_t earthLattitudeAngle = Date::JDToHourOfDay(Date::AipsMJDToJD(time))*M_PIn/12.0L; // Rotate baseline plane towards source, first rotate around x axis, then around z axis num_t raRotation = earthLattitudeAngle - pointingLattitude + M_PIn*0.5L; num_t raCos = cosn(-raRotation); num_t raSin = sinn(-raRotation); num_t dx = baseline.antenna1.x - baseline.antenna2.x; num_t dy = baseline.antenna1.y - baseline.antenna2.y; num_t dz = baseline.antenna1.z - baseline.antenna2.z; num_t decCos = cosn(delayDirectionDec); num_t decSin = sinn(delayDirectionDec); num_t du = -dx * raCos * decSin - dy * raSin - dz * raCos * decCos, dv = -dx * raSin * decSin + dy * raCos - dz * raSin * decCos; /* num_t dxProjected = tmpCos*dx - tmpSin*dy; num_t tmpdy = tmpSin*dx + tmpCos*dy; num_t dyProjected = tmpCos*tmpdy - tmpSin*dz;*/ // du = dx*cosn(ra) - dy*sinn(ra) // dv = ( dx*sinn(ra) + dy*cosn(ra) ) * cosn(-dec) - dz * sinn(-dec) // Now, the newly projected positive z axis of the baseline points to the field num_t baselineLength = sqrtn(du*du + dv*dv); num_t baselineAngle; if(baselineLength == 0.0) baselineAngle = 0.0; else { baselineLength *= frequency / SpeedOfLight(); if(du > 0.0L) baselineAngle = atann(du/dv); else baselineAngle = M_PIn - atann(du/-dv); } u = cosn(baselineAngle)*baselineLength; v = -sinn(baselineAngle)*baselineLength; std::cout << "Calced: " << u << "," << v << ", ori: " << metaData->UVW()[timeIndex].u << "," << metaData->UVW()[timeIndex].v << "(," << metaData->UVW()[timeIndex].w << ")\n"; }
void SinusFitter::FindPhaseAndAmplitude(num_t &phase, num_t &litude, const num_t *dataX, const num_t *dataT, const size_t dataSize, const num_t frequency) const throw() { // calculate 1/N * \sum_t x(t) e^{-i * frequency * t} num_t sumR = 0.0L, sumI = 0.0L; for(unsigned i=0;i<dataSize;++i) { const num_t t = dataT[i]; const num_t x = dataX[i]; sumR += x * cosn(-t * frequency); sumI += x * sinn(-t * frequency); } sumR /= (num_t) dataSize; sumI /= (num_t) dataSize; phase = Phase(sumR, sumI); amplitude = 2.0L * sqrtn(sumR*sumR + sumI*sumI); }
void UVImager::GetUVPosition(num_t &u, num_t &v, const SingleFrequencySingleBaselineData &data, const AntennaCache &cache) { unsigned field = data.field; num_t pointingLattitude = _fields[field].delayDirectionRA; //calcTimer.Start(); num_t earthLattitudeAngle = Date::JDToHourOfDay(Date::AipsMJDToJD(data.time))*M_PIn/12.0L; //long double pointingLongitude = _fields[field].delayDirectionDec; //not used // Rotate baseline plane towards source, first rotate around z axis, then around x axis num_t raRotation = earthLattitudeAngle - pointingLattitude + M_PIn*0.5L; num_t tmpCos = cosn(raRotation); num_t tmpSin = sinn(raRotation); num_t dxProjected = tmpCos*cache.dx - tmpSin*cache.dy; num_t tmpdy = tmpSin*cache.dx + tmpCos*cache.dy; tmpCos = _fields[field].delayDirectionDecNegCos; // cosn(-pointingLongitude); tmpSin = _fields[field].delayDirectionDecNegSin; //sinn(-pointingLongitude); num_t dyProjected = tmpCos*tmpdy - tmpSin*cache.dz; // long double dzProjected = tmpSin*tmpdy + tmpCos*dzAnt; // we don't need it // Now, the newly projected positive z axis of the baseline points to the field num_t baselineLength = sqrtn(dxProjected*dxProjected + dyProjected*dyProjected); num_t baselineAngle; if(baselineLength == 0.0L) baselineAngle = 0.0L; else { baselineLength /= cache.wavelength; if(dxProjected > 0.0L) baselineAngle = atann(dyProjected/dxProjected); else baselineAngle = M_PIn - atann(dyProjected/-dxProjected); } u = cosn(baselineAngle)*baselineLength; v = -sinn(baselineAngle)*baselineLength; }
Image2D TestSetGenerator::MakeTestSet(int number, Mask2D& rfi, unsigned width, unsigned height, int gaussianNoise) { Image2D image; switch(number) { case 0: // Image of all zero's return Image2D::MakeZeroImage(width, height); case 1: // Image of all ones image = Image2D::MakeUnsetImage(width, height); image.SetAll(1.0); break; case 2: // Noise return MakeNoise(width, height, gaussianNoise); case 3: { // Several broadband lines image = MakeNoise(width, height, gaussianNoise); AddBroadbandToTestSet(image, rfi, 1.0); } break; case 4: { // Several broadband lines image = MakeNoise(width, height, gaussianNoise); AddBroadbandToTestSet(image, rfi, 0.5); } break; case 5: { // Several broadband lines of random length image = MakeNoise(width, height, gaussianNoise); AddVarBroadbandToTestSet(image, rfi); } break; case 6: { // Different broadband lines + low freq background image = MakeNoise(width, height, gaussianNoise); AddVarBroadbandToTestSet(image, rfi); for(unsigned y=0;y<image.Height();++y) { for(unsigned x=0;x<image.Width();++x) { image.AddValue(x, y, sinn(num_t(x)*M_PIn*5.0 / image.Width()) + 0.1); } } } break; case 7: { // Different broadband lines + high freq background image = MakeNoise(width, height, gaussianNoise); for(unsigned y=0;y<image.Height();++y) { for(unsigned x=0;x<image.Width();++x) { image.AddValue(x, y, sinn((long double) (x+y*0.1)*M_PIn*5.0L / image.Width() + 0.1)); image.AddValue(x, y, sinn((long double) (x+pown(y, 1.1))*M_PIn*50.0L / image.Width() + 0.1)); } } AddVarBroadbandToTestSet(image, rfi); for(unsigned y=0;y<image.Height();++y) { for(unsigned x=0;x<image.Width();++x) { image.AddValue(x, y, 1.0); } } } break; case 8: { // Different droadband lines + smoothed&subtracted high freq background image = MakeNoise(width, height, gaussianNoise); for(unsigned y=0;y<image.Height();++y) { for(unsigned x=0;x<image.Width();++x) { image.AddValue(x, y, sinn((num_t) (x+y*0.1)*M_PIn*5.0 / image.Width() + 0.1)); image.AddValue(x, y, sinn((num_t) (x+pown(y, 1.1))*M_PIn*50.0 / image.Width() + 0.1)); } } SubtractBackground(image); AddVarBroadbandToTestSet(image, rfi); } break; case 9: { //FFT of 7 image = MakeTestSet(7, rfi, width, height); Image2D copy(image); FFTTools::CreateHorizontalFFTImage(image, copy, false); for(unsigned y=0;y<rfi.Height();++y) { for(unsigned x=0;x<rfi.Width();++x) { image.SetValue(x, y, image.Value(x, y) / sqrtn(image.Width())); } } } break; case 10: { // Identity matrix image = Image2D::MakeZeroImage(width, height); unsigned min = width < height ? width : height; for(unsigned i=0;i<min;++i) { image.SetValue(i, i, 1.0); rfi.SetValue(i, i, true); } } break; case 11: { // FFT of identity matrix image = MakeTestSet(10, rfi, width, height); Image2D copy(image); FFTTools::CreateHorizontalFFTImage(image, copy, false); for(unsigned y=0;y<rfi.Height();++y) { for(unsigned x=0;x<rfi.Width();++x) { image.SetValue(x, y, image.Value(x, y) / sqrtn(width)); } } } break; case 12: { // Broadband contaminating all channels image = MakeNoise(width, height, gaussianNoise); for(unsigned y=0;y<image.Height();++y) { for(unsigned x=0;x<image.Width();++x) { image.AddValue(x, y, sinn((num_t) (x+y*0.1)*M_PIn*5.0 / image.Width() + 0.1)); image.AddValue(x, y, sinn((num_t) (x+powl(y, 1.1))*M_PIn*50.0 / image.Width() + 0.1)); } } AddBroadbandToTestSet(image, rfi, 1.0); } break; case 13: { // Model of three point sources with broadband RFI SetModelData(image, rfi, 3); Image2D noise = MakeNoise(image.Width(), image.Height(), gaussianNoise); image += noise; AddBroadbandToTestSet(image, rfi, 1.0); } break; case 14: { // Model of five point sources with broadband RFI SetModelData(image, rfi, 5); Image2D noise = MakeNoise(image.Width(), image.Height(), gaussianNoise); image += noise; AddBroadbandToTestSet(image, rfi, 1.0); } break; case 15: { // Model of five point sources with partial broadband RFI SetModelData(image, rfi, 5); Image2D noise = MakeNoise(image.Width(), image.Height(), gaussianNoise); image += noise; AddBroadbandToTestSet(image, rfi, 0.5); } break; case 16: { // Model of five point sources with random broadband RFI SetModelData(image, rfi, 5); Image2D noise = MakeNoise(image.Width(), image.Height(), gaussianNoise); image += noise; AddVarBroadbandToTestSet(image, rfi); } break; case 17: { // Background-fitted model of five point sources with random broadband RFI SetModelData(image, rfi, 5); Image2D noise = MakeNoise(image.Width(), image.Height(), gaussianNoise); image += noise; SubtractBackground(image); AddVarBroadbandToTestSet(image, rfi); } break; case 18: { // Model of three point sources with random RFI SetModelData(image, rfi, 3); Image2D noise = MakeNoise(image.Width(), image.Height(), gaussianNoise); image += noise; AddVarBroadbandToTestSet(image, rfi); } break; case 19: { // Model of three point sources with noise SetModelData(image, rfi, 3); Image2D noise = MakeNoise(image.Width(), image.Height(), gaussianNoise); image += noise; } break; case 20: { // Model of five point sources with noise SetModelData(image, rfi, 5); Image2D noise = MakeNoise(image.Width(), image.Height(), gaussianNoise); image += noise; } break; case 21: { // Model of three point sources SetModelData(image, rfi, 3); } break; case 22: { // Model of five point sources image = Image2D::MakeZeroImage(width, height); SetModelData(image, rfi, 5); } break; case 23: image = MakeNoise(width, height, gaussianNoise); AddBroadbandToTestSet(image, rfi, 0.5, 0.1, true); break; case 24: image = MakeNoise(width, height, gaussianNoise); AddBroadbandToTestSet(image, rfi, 0.5, 10.0, true); break; case 25: image = MakeNoise(width, height, gaussianNoise); AddBroadbandToTestSet(image, rfi, 0.5, 1.0, true); break; case 26: { // Several Gaussian broadband lines image = MakeNoise(width, height, gaussianNoise); AddBroadbandToTestSet(image, rfi, 1.0, 1.0, false, GaussianShape); } break; case 27: { // Several Sinusoidal broadband lines image = MakeNoise(width, height, gaussianNoise); AddBroadbandToTestSet(image, rfi, 1.0, 1.0, false, SinusoidalShape); } break; case 28: { // Several slewed Gaussian broadband lines image = MakeNoise(width, height, gaussianNoise); AddSlewedBroadbandToTestSet(image, rfi, 1.0); } break; case 29: { // Several bursty broadband lines image = MakeNoise(width, height, gaussianNoise); AddBurstBroadbandToTestSet(image, rfi); } break; case 30: { // noise + RFI ^-2 distribution image = sampleRFIDistribution(width, height, 1.0); rfi.SetAll<true>(); } break; case 31: { // noise + RFI ^-2 distribution image = sampleRFIDistribution(width, height, 0.1); rfi.SetAll<true>(); } break; case 32: { // noise + RFI ^-2 distribution image = sampleRFIDistribution(width, height, 0.01); rfi.SetAll<true>(); } break; } return image; }
void LVector::mBasis( const tmv::ConstVectorView<double>& x, const tmv::ConstVectorView<double>& y, const tmv::ConstVectorView<double>* invsig, tmv::MatrixView<T> psi, int order, double sigma) { assert (y.size()==x.size()); assert (psi.nrows()==x.size() && psi.ncols()==PQIndex::size(order)); const int N=order; const int npts_full = x.size(); // It's faster to build the psi matrix in blocks so that more of the matrix stays in // L1 cache. For a (typical) 256 KB L2 cache size, this corresponds to 8 columns in the // cache, which is pretty good, since we are usually working on 4 columns at a time, // plus either X and Y or 3 Lq vectors. const int BLOCKING_FACTOR=4096; const int max_npts = std::max(BLOCKING_FACTOR,npts_full); tmv::DiagMatrix<double> Rsq_full(max_npts); tmv::Matrix<double> A_full(max_npts,2); tmv::Matrix<double> tmp_full(max_npts,2); tmv::DiagMatrix<double> Lmq_full(max_npts); tmv::DiagMatrix<double> Lmqm1_full(max_npts); tmv::DiagMatrix<double> Lmqm2_full(max_npts); for (int ilo=0; ilo<npts_full; ilo+=BLOCKING_FACTOR) { const int ihi = std::min(npts_full, ilo + BLOCKING_FACTOR); const int npts = ihi-ilo; // Cast arguments as diagonal matrices so we can access // vectorized element-by-element multiplication tmv::ConstDiagMatrixView<double> X = DiagMatrixViewOf(x.subVector(ilo,ihi)); tmv::ConstDiagMatrixView<double> Y = DiagMatrixViewOf(y.subVector(ilo,ihi)); // Get the appropriate portion of our temporary matrices. tmv::DiagMatrixView<double> Rsq = Rsq_full.subDiagMatrix(0,npts); tmv::MatrixView<double> A = A_full.rowRange(0,npts); tmv::MatrixView<double> tmp = tmp_full.rowRange(0,npts); // We need rsq values twice, so store them here. Rsq = X*X; Rsq += Y*Y; // This matrix will keep track of real & imag parts // of prefactor * exp(-r^2/2) (x+iy)^m / sqrt(m!) // Build the Gaussian factor for (int i=0; i<npts; i++) A.ref(i,0) = std::exp(-0.5*Rsq(i)); mBasisHelper<T>::applyPrefactor(A.col(0),sigma); A.col(1).setZero(); // Put 1/sigma factor into every point if doing a design matrix: if (invsig) A.col(0) *= tmv::DiagMatrixViewOf(invsig->subVector(ilo,ihi)); // Assign the m=0 column first: psi.col( PQIndex(0,0).rIndex(), ilo,ihi ) = A.col(0); // Then ascend m's at q=0: for (int m=1; m<=N; m++) { int rIndex = PQIndex(m,0).rIndex(); // Multiply by (X+iY)/sqrt(m), including a factor 2 first time through tmp = Y * A; A = X * A; A.col(0) += tmp.col(1); A.col(1) -= tmp.col(0); A *= m==1 ? 2. : 1./sqrtn(m); psi.subMatrix(ilo,ihi,rIndex,rIndex+2) = mBasisHelper<T>::Asign(m%4) * A; } // Make three DiagMatrix to hold Lmq's during recurrence calculations boost::shared_ptr<tmv::DiagMatrixView<double> > Lmq( new tmv::DiagMatrixView<double>(Lmq_full.subDiagMatrix(0,npts))); boost::shared_ptr<tmv::DiagMatrixView<double> > Lmqm1( new tmv::DiagMatrixView<double>(Lmqm1_full.subDiagMatrix(0,npts))); boost::shared_ptr<tmv::DiagMatrixView<double> > Lmqm2( new tmv::DiagMatrixView<double>(Lmqm2_full.subDiagMatrix(0,npts))); for (int m=0; m<=N; m++) { PQIndex pq(m,0); int iQ0 = pq.rIndex(); // Go to q=1: pq.incN(); if (pq.pastOrder(N)) continue; { // q == 1 const int p = pq.getP(); const int q = pq.getQ(); const int iQ = pq.rIndex(); Lmqm1->setAllTo(1.); // This is Lm0. *Lmq = Rsq - (p+q-1.); *Lmq *= mBasisHelper<T>::Lsign(1.) / (sqrtn(p)*sqrtn(q)); if (m==0) { psi.col(iQ,ilo,ihi) = (*Lmq) * psi.col(iQ0,ilo,ihi); } else { psi.subMatrix(ilo,ihi,iQ,iQ+2) = (*Lmq) * psi.subMatrix(ilo,ihi,iQ0,iQ0+2); } } // do q=2,... for (pq.incN(); !pq.pastOrder(N); pq.incN()) { const int p = pq.getP(); const int q = pq.getQ(); const int iQ = pq.rIndex(); // cycle the Lmq vectors // Lmqm2 <- Lmqm1 // Lmqm1 <- Lmq // Lmq <- Lmqm2 Lmqm2.swap(Lmqm1); Lmqm1.swap(Lmq); double invsqrtpq = 1./sqrtn(p)/sqrtn(q); *Lmq = Rsq - (p+q-1.); *Lmq *= mBasisHelper<T>::Lsign(invsqrtpq) * *Lmqm1; *Lmq -= (sqrtn(p-1)*sqrtn(q-1)*invsqrtpq) * (*Lmqm2); if (m==0) { psi.col(iQ,ilo,ihi) = (*Lmq) * psi.col(iQ0,ilo,ihi); } else { psi.subMatrix(ilo,ihi,iQ,iQ+2) = (*Lmq) * psi.subMatrix(ilo,ihi,iQ0,iQ0+2); } } } } }