修改傅里叶变换bug

pull/5/head
chenzh 2025-02-26 19:39:46 +08:00
parent 57e998686c
commit 1dd16d0727
5 changed files with 147 additions and 111 deletions

View File

@ -516,11 +516,11 @@ extern "C" void CUDAIFFT(cuComplex* inArr, cuComplex* outArr, long InRowCount,
int rank = 1;
int n[] = { InColCount }; // 每个IFFT处理freqcount点
int inembed[] = { InColCount };
int onembed[] = { InColCount };
int onembed[] = { outColCount };
int istride = 1;
int ostride = 1;
int idist = InColCount; // 输入批次间距
int odist = InColCount; // 输出批次间距
int odist = outColCount; // 输出批次间距
int batch = InRowCount; // 批处理数量
result = cufftPlanMany(&plan, rank, n,
@ -536,7 +536,7 @@ extern "C" void CUDAIFFT(cuComplex* inArr, cuComplex* outArr, long InRowCount,
cuComplex* in_ptr = inArr;
cuComplex* out_ptr = outArr;
result = cufftExecC2C(plan, (cufftComplex*)in_ptr, (cufftComplex*)out_ptr, CUFFT_INVERSE);
if (result != CUFFT_SUCCESS) {
cufftDestroy(plan);
return;

View File

@ -1,5 +1,6 @@
#include "RasterWidgetMessageShow.h"
#include <stdio.h>
#include <iostream>
namespace RasterMessageShow {
RasterWidgetMessageShow* RasterWidgetMessageShow::_instance = nullptr;
@ -27,9 +28,10 @@ namespace RasterMessageShow {
{
if (nullptr != this->textBrowserMessage) {
this->textBrowserMessage->append(Message);
this->textBrowserMessage->moveCursor(QTextCursor::MoveOperation::End);
this->textBrowserMessage->repaint();
std::cout << Message.toLocal8Bit().constData() << std::endl;
}
else {}
}

View File

@ -33,7 +33,7 @@ __device__ __host__ Vector3 vec_cross(Vector3 a, Vector3 b) {
__device__ __host__ Vector3 vec_normalize(Vector3 v) {
double len = sqrt(vec_dot(v, v));
return (len > 1e-12) ? (Vector3) { v.x / len, v.y / len, v.z / len } : v;
return (len > 1e-12) ? Vector3 { v.x / len, v.y / len, v.z / len } : v;
}
// 计算视线交点T
@ -46,18 +46,18 @@ extern __device__ __host__ Vector3 compute_T(Vector3 S, Vector3 ray, double H) {
double C = (S.x * S.x + S.y * S.y) / (a_h * a_h) + S.z * S.z / (WGS84_B * WGS84_B) - 1.0;
double disc = B * B - 4 * A * C;
if (disc < 0) return (Vector3) { NAN, NAN, NAN };
if (disc < 0) return Vector3 { NAN, NAN, NAN };
double sqrt_d = sqrt(disc);
double t = fmax((-B - sqrt_d) / (2 * A), (-B + sqrt_d) / (2 * A));
return (t > 1e-6) ? (Vector3) { S.x + dir.x * t, S.y + dir.y * t, S.z + dir.z * t }
: (Vector3) { NAN, NAN, NAN };
return (t > 1e-6) ? Vector3 { S.x + dir.x * t, S.y + dir.y * t, S.z + dir.z * t }
: Vector3 { NAN, NAN, NAN };
}
// 构建平面基底
extern __device__ __host__ void compute_basis(Vector3 S, Vector3 T, Vector3* e1, Vector3* e2) {
Vector3 ST = vec_normalize(vec_sub(T, S));
Vector3 SO = vec_normalize(vec_sub((Vector3) { 0, 0, 0 }, S)); // S->O方向
Vector3 SO = vec_normalize(vec_sub(Vector3 { 0, 0, 0 }, S)); // S->O方向
*e1 = vec_normalize(vec_cross(ST, SO));
*e2 = vec_normalize(vec_cross(*e1, ST));
@ -108,7 +108,7 @@ extern __device__ __host__ Vector3 compute_P(Vector3 S, Vector3 T, double R, dou
// 计算参考角度方向
Vector3 ST_vec = vec_sub(T, S);
Vector3 SO_vec = vec_sub((Vector3) { 0, 0, 0 }, S);
Vector3 SO_vec = vec_sub(Vector3 { 0, 0, 0 }, S);
Vector3 ref_cross = vec_cross(SO_vec, ST_vec);
double ref_sign = ref_cross.z; // 取Z分量判断方向

View File

@ -33,17 +33,17 @@ __global__ void kernel_TimeBPImageGridNet(double* antPx, double* antPy, double*
if (idx < pixelcount) {
// 计算坐标
Vector3 S = { antPx[idx], antPy[idx], antPz[idx] }; // 卫星位置 (m)
Vector3 ray = { antDirx[idx], antDiry[idx], antDirz[idx] }; // 视线方向
Vector3 S = { antPx[prfid], antPy[prfid], antPz[prfid] }; // 卫星位置 (m)
Vector3 ray = { antDirx[prfid], antDiry[prfid], antDirz[prfid] }; // 视线方向
double H = meanH; // 平均高程
double R = Rnear+ dx*Rid; // 目标距离
// 参数校验
if (R <= 0 || H < -WGS84_A * 0.1 || H > WGS84_A * 0.1) {
//printf("参数错误:\n H范围±%.1f km\n R必须>0\n", WGS84_A * 0.1 / 1000);
imgx[idx] = 0;
imgy[idx] = 0;
imgz[idx] = 0;
imgx[idx] = 1.0/0;
imgy[idx] = 1.0/0;
imgz[idx] = 1.0/0;
return ;
}
@ -51,9 +51,9 @@ __global__ void kernel_TimeBPImageGridNet(double* antPx, double* antPy, double*
// Step 1: 计算交点T
Vector3 T = compute_T(S, ray, H);
if (isnan(T.x)) {
imgx[idx] = 0;
imgy[idx] = 0;
imgz[idx] = 0;
imgx[idx] = 1.0/0;
imgy[idx] = 1.0/0;
imgz[idx] = 1.0/0;
return ;
}
@ -68,9 +68,9 @@ __global__ void kernel_TimeBPImageGridNet(double* antPx, double* antPy, double*
}
else {
imgx[idx] = 0;
imgy[idx] = 0;
imgz[idx] = 0;
imgx[idx] = 1.0/0;
imgy[idx] = 1.0/0;
imgz[idx] = 1.0/0;
//printf("未找到有效解\n");
}
}

View File

@ -12,6 +12,13 @@
void CreatePixelXYZ(std::shared_ptr<EchoL0Dataset> echoL0ds, QString outPixelXYZPath)
{
long bandwidth = echoL0ds->getBandwidth();
double Rnear = echoL0ds->getNearRange();
double Rfar = echoL0ds->getFarRange();
double refRange = echoL0ds->getRefPhaseRange();
double dx = LIGHTSPEED / 2.0 / bandwidth; // c/2b
// 创建坐标系统
long prfcount = echoL0ds->getPluseCount();
long freqcount = echoL0ds->getPlusePoints();
@ -24,91 +31,107 @@ void CreatePixelXYZ(std::shared_ptr<EchoL0Dataset> echoL0ds, QString outPixelXYZ
gt(1, 2) = 1;
gdalImage xyzRaster = CreategdalImage(outPixelXYZPath, prfcount, freqcount, 3, gt, QString(""), false, true,true);
std::shared_ptr<double> antpos = echoL0ds->getAntPos();
double dx = (echoL0ds->getFarRange()-echoL0ds->getNearRange())/(echoL0ds->getPlusePoints()-1);
double Rnear = echoL0ds->getNearRange();
dx = (echoL0ds->getFarRange()-echoL0ds->getNearRange())/(echoL0ds->getPlusePoints()-1);
Rnear = echoL0ds->getNearRange();
double Rref = echoL0ds->getRefPhaseRange();
double centerInc = echoL0ds->getCenterAngle()*d2r;
long echocol = 1073741824 / 8 / 4 / prfcount*8;
long echocol = Memory1GB * 1.0 / 8 / 4 / prfcount * 6;
qDebug() << "echocol:\t " << echocol ;
echocol = echocol < 3000 ? 3000 : echocol;
long startcolidx = 0;
for (startcolidx = 0; startcolidx < freqcount; startcolidx = startcolidx + echocol) {
echocol = echocol < 1 ? 1: echocol;
std::shared_ptr<double> Pxs((double*)mallocCUDAHost(sizeof(double)*prfcount), FreeCUDAHost);
std::shared_ptr<double> Pys((double*)mallocCUDAHost(sizeof(double) * prfcount), FreeCUDAHost);
std::shared_ptr<double> Pzs((double*)mallocCUDAHost(sizeof(double) * prfcount), FreeCUDAHost);
std::shared_ptr<double> AntDirectX((double*)mallocCUDAHost(sizeof(double) * prfcount), FreeCUDAHost);
std::shared_ptr<double> AntDirectY((double*)mallocCUDAHost(sizeof(double) * prfcount), FreeCUDAHost);
std::shared_ptr<double> AntDirectZ((double*)mallocCUDAHost(sizeof(double) * prfcount), FreeCUDAHost);
{
std::shared_ptr<double> antpos = echoL0ds->getAntPos();
double time = 0;
double Px = 0;
double Py = 0;
double Pz = 0;
for (long i = 0; i < prfcount; i++) {
Pxs.get()[i] = antpos.get()[i * 19 + 1]; // 卫星坐标
Pys.get()[i] = antpos.get()[i * 19 + 2];
Pzs.get()[i] = antpos.get()[i * 19 + 3];
AntDirectX.get()[i] = antpos.get()[i * 19 + 13];// zero doppler
AntDirectY.get()[i] = antpos.get()[i * 19 + 14];
AntDirectZ.get()[i] = antpos.get()[i * 19 + 15];
double NormAnt = std::sqrt(AntDirectX.get()[i] * AntDirectX.get()[i] +
AntDirectY.get()[i] * AntDirectY.get()[i] +
AntDirectZ.get()[i] * AntDirectZ.get()[i]);
AntDirectX.get()[i] = AntDirectX.get()[i] / NormAnt;
AntDirectY.get()[i] = AntDirectY.get()[i] / NormAnt;
AntDirectZ.get()[i] = AntDirectZ.get()[i] / NormAnt;// 归一化
}
antpos.reset();
}
std::shared_ptr<double> d_Pxs((double*)mallocCUDADevice(sizeof(double) * prfcount), FreeCUDADevice);
std::shared_ptr<double> d_Pys((double*)mallocCUDADevice(sizeof(double) * prfcount), FreeCUDADevice);
std::shared_ptr<double> d_Pzs((double*)mallocCUDADevice(sizeof(double) * prfcount), FreeCUDADevice);
std::shared_ptr<double> d_AntDirectX((double*)mallocCUDADevice(sizeof(double) * prfcount), FreeCUDADevice);
std::shared_ptr<double> d_AntDirectY((double*)mallocCUDADevice(sizeof(double) * prfcount), FreeCUDADevice);
std::shared_ptr<double> d_AntDirectZ((double*)mallocCUDADevice(sizeof(double) * prfcount), FreeCUDADevice);
HostToDevice(Pxs.get(), d_Pxs.get(), sizeof(double) * prfcount);
HostToDevice(Pys.get(), d_Pys.get(), sizeof(double) * prfcount);
HostToDevice(Pzs.get(), d_Pzs.get(), sizeof(double) * prfcount);
HostToDevice(d_AntDirectX.get(), d_AntDirectX.get(), sizeof(double) * prfcount);
HostToDevice(d_AntDirectY.get(), d_AntDirectY.get(), sizeof(double) * prfcount);
HostToDevice(d_AntDirectZ.get(), d_AntDirectZ.get(), sizeof(double) * prfcount);
for (long startcolidx = 0; startcolidx < freqcount; startcolidx = startcolidx + echocol) {
long tempechocol = echocol;
if (startcolidx + tempechocol >= freqcount) {
tempechocol = freqcount - startcolidx;
}
qDebug() << "\r[" << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz") << "] imgxyz :\t" << startcolidx << "\t-\t" << startcolidx + tempechocol << " / " << freqcount ;
std::shared_ptr<double> demx = readDataArr<double>(xyzRaster, 0, startcolidx, prfcount, tempechocol, 1, GDALREADARRCOPYMETHOD::VARIABLEMETHOD);
std::shared_ptr<double> demy = readDataArr<double>(xyzRaster, 0, startcolidx, prfcount, tempechocol, 2, GDALREADARRCOPYMETHOD::VARIABLEMETHOD);
std::shared_ptr<double> demz = readDataArr<double>(xyzRaster, 0, startcolidx, prfcount, tempechocol, 3, GDALREADARRCOPYMETHOD::VARIABLEMETHOD);
Eigen::MatrixXd demx = xyzRaster.getData(0, startcolidx, prfcount, tempechocol, 1);
Eigen::MatrixXd demy = xyzRaster.getData(0, startcolidx, prfcount, tempechocol, 2);
Eigen::MatrixXd demz = xyzRaster.getData(0, startcolidx, prfcount, tempechocol, 3);
std::shared_ptr<double> h_demx((double*)mallocCUDAHost(sizeof(double) * prfcount*tempechocol), FreeCUDAHost);
std::shared_ptr<double> h_demy((double*)mallocCUDAHost(sizeof(double) * prfcount*tempechocol), FreeCUDAHost);
std::shared_ptr<double> h_demz((double*)mallocCUDAHost(sizeof(double) * prfcount*tempechocol), FreeCUDAHost);
#pragma omp parallel for
for (long i = 0; i < prfcount; i++) {
double Px = 0;
double Py = 0;
double Pz = 0;
double AntDirectX = 0;
double AntDirectY = 0;
double AntDirectZ = 0;
double R = 0;
double NormAnt = 0;
Px = antpos.get()[i * 19 + 1]; // 卫星坐标
Py = antpos.get()[i * 19 + 2];
Pz = antpos.get()[i * 19 + 3];
AntDirectX = antpos.get()[i * 19 + 13];// zero doppler
AntDirectY = antpos.get()[i * 19 + 14];
AntDirectZ = antpos.get()[i * 19 + 15];
NormAnt = std::sqrt(AntDirectX * AntDirectX + AntDirectY * AntDirectY + AntDirectZ * AntDirectZ);
AntDirectX = AntDirectX / NormAnt;
AntDirectY = AntDirectY / NormAnt;
AntDirectZ = AntDirectZ / NormAnt;// 归一化
// 计算中心参考点
double centerX = Px + Rref * AntDirectX; // T1
double centerY = Py + Rref * AntDirectY;
double centerZ = Pz + Rref * AntDirectZ;
double satH = Rref * std::cos(centerInc); // 卫星高
double PR = sqrt(Px * Px + Py * Py + Pz * Pz);
double satR = PR - satH;
double sPx = satR / PR * Px;
double sPy = satR / PR * Py;
double sPz = satR / PR * Pz;
double dTSx = sPx - centerX;
double dTSy = sPy - centerY;
double dTSz = sPz - centerZ;
double dTSR = sqrt(dTSx * dTSx + dTSy * dTSy + dTSz * dTSz);
dTSx=dTSx/dTSR;
dTSy=dTSy/dTSR;
dTSz=dTSz/dTSR;
for (long j = 0; j < tempechocol; j++) {
R = (j + startcolidx)*dx + Rnear;
double dRp = (Rref - R) / sin(centerInc); // -- 0 +++
demx(i,j) = centerX + dTSx * dRp;
demy(i,j) = centerY + dTSy * dRp;
demz(i,j) = centerZ + dTSz * dRp;
#pragma omp parallel for
for (long ii = 0; ii < prfcount; ii++) {
for (long jj = 0; jj < tempechocol; jj++) {
h_demx.get()[ii*tempechocol+jj]=demx.get()[ii*tempechocol+jj];
h_demy.get()[ii*tempechocol+jj]=demy.get()[ii*tempechocol+jj];
h_demz.get()[ii*tempechocol+jj]=demz.get()[ii*tempechocol+jj];
}
}
xyzRaster.saveImage(demx, 0, startcolidx, 1);
xyzRaster.saveImage(demy, 0, startcolidx, 2);
xyzRaster.saveImage(demz, 0, startcolidx, 3);
std::shared_ptr<double> d_demx((double*)mallocCUDADevice(sizeof(double) * prfcount * tempechocol), FreeCUDADevice);
std::shared_ptr<double> d_demy((double*)mallocCUDADevice(sizeof(double) * prfcount * tempechocol), FreeCUDADevice);
std::shared_ptr<double> d_demz((double*)mallocCUDADevice(sizeof(double) * prfcount * tempechocol), FreeCUDADevice);
HostToDevice(h_demx.get(), d_demx.get(), sizeof(double) * prfcount * tempechocol);
HostToDevice(h_demy.get(), d_demy.get(), sizeof(double) * prfcount * tempechocol);
HostToDevice(h_demz.get(), d_demz.get(), sizeof(double) * prfcount * tempechocol);
TIMEBPCreateImageGrid(
d_Pxs.get(), d_Pys.get(), d_Pzs.get(),
d_AntDirectX.get(), d_AntDirectY.get(), d_AntDirectZ.get(),
d_demx.get(), d_demy.get(), d_demz.get(),
prfcount, tempechocol, 0,
Rnear, dx, refRange
);
xyzRaster.saveImage(demx, 0, startcolidx,prfcount,tempechocol, 1);
xyzRaster.saveImage(demy, 0, startcolidx,prfcount,tempechocol, 2);
xyzRaster.saveImage(demz, 0, startcolidx,prfcount,tempechocol, 3);
}
}
@ -171,11 +194,18 @@ std::shared_ptr<SARSimulationImageL1Dataset> TBPImageAlgCls::getImageL0()
ErrorCode TBPImageAlgCls::Process(long num_thread)
{
qDebug() << u8"开始成像";
qDebug() << u8"频域回波-> 时域回波";
this->TimeEchoDataPath = JoinPath(this->L1ds->getoutFolderPath(), this->L0ds->getSimulationTaskName() + "_Timeecho.bin");
this->EchoFreqToTime();
qDebug() << u8"创建成像平面的XYZ";
QString outRasterXYZ = JoinPath(this->L1ds->getoutFolderPath(), this->L0ds->getSimulationTaskName() + "_xyz.bin");
CreatePixelXYZ(this->L0ds, outRasterXYZ);
this->outRasterXYZPath = outRasterXYZ;
// 初始化Raster
qDebug() << u8"初始化影像";
long imageheight = this->L1ds->getrowCount();
@ -202,10 +232,7 @@ ErrorCode TBPImageAlgCls::Process(long num_thread)
}
this->L1ds->saveImageRaster(imageRaster, startline,templine);
}
qDebug() << u8"开始成像";
qDebug() << u8"频域回波-> 时域回波";
this->TimeEchoDataPath = JoinPath(this->L1ds->getoutFolderPath(), this->L0ds->getSimulationTaskName() + "_Timeecho.bin");
this->EchoFreqToTime();
qDebug() << u8"频域回波-> 时域回波 结束";
@ -260,9 +287,9 @@ ErrorCode TBPImageAlgCls::ProcessGPU()
qDebug() << "deltaF:\t" << deltaF;
std::shared_ptr<double> Pxs (new double[this->L0ds->getPluseCount()]);
std::shared_ptr<double> Pys (new double[this->L0ds->getPluseCount()]);
std::shared_ptr<double> Pzs (new double[this->L0ds->getPluseCount()]);
std::shared_ptr<double> Pxs (new double[this->L0ds->getPluseCount()],delArrPtr);
std::shared_ptr<double> Pys (new double[this->L0ds->getPluseCount()],delArrPtr);
std::shared_ptr<double> Pzs (new double[this->L0ds->getPluseCount()],delArrPtr);
{
std::shared_ptr<double> antpos = this->L0ds->getAntPos();
@ -337,9 +364,9 @@ ErrorCode TBPImageAlgCls::ProcessGPU()
tempechoBlockline = PRFCount - startechoid;
}
std::shared_ptr<std::complex<double>> echoArr = this->L0ds->getEchoArr(startechoid, tempechoBlockline);
std::shared_ptr<double> antpx(new double[tempechoBlockline*PlusePoints],delArrPtr);
std::shared_ptr<double> antpy(new double[tempechoBlockline* PlusePoints], delArrPtr);
std::shared_ptr<double> antpz(new double[tempechoBlockline* PlusePoints], delArrPtr);
std::shared_ptr<double> antpx(new double[tempechoBlockline],delArrPtr);
std::shared_ptr<double> antpy(new double[tempechoBlockline], delArrPtr);
std::shared_ptr<double> antpz(new double[tempechoBlockline], delArrPtr);
// 复制
for (long anti = 0; anti < tempechoBlockline; anti++) {
antpx.get()[anti] = Pxs.get()[anti + startechoid];
@ -384,7 +411,7 @@ void TBPImageAlgCls::EchoFreqToTime( )
gdalImageComplex outTimeEchoImg = CreategdalImageComplexNoProj(this->TimeEchoDataPath,this->TimeEchoRowCount,this->TimeEchoColCount,1);
// 分块
long echoBlockline = Memory1GB / 8 / 2 / outColCount * 1; //1GB
long echoBlockline = Memory1GB / 8 / 2 / outColCount * 2; //1GB
echoBlockline = echoBlockline < 1 ? 1 : echoBlockline;
@ -398,16 +425,20 @@ void TBPImageAlgCls::EchoFreqToTime( )
std::shared_ptr<std::complex<double>> echoArr = this->L0ds->getEchoArr(startechoid, tempechoBlockline);
std::shared_ptr<std::complex<double>> IFFTArr = outTimeEchoImg.getDataComplexSharePtr(startechoid, 0, tempechoBlockline, outColCount, 1);
std::shared_ptr<cuComplex> host_echoArr((cuComplex*)mallocCUDAHost(sizeof(cuComplex)* tempechoBlockline * inColCount), FreeCUDAHost);
std::shared_ptr<cuComplex> host_echoArr((cuComplex*)mallocCUDAHost(sizeof(cuComplex)* tempechoBlockline * outColCount), FreeCUDAHost);
std::shared_ptr<cuComplex> host_IFFTechoArr((cuComplex*)mallocCUDAHost(sizeof(cuComplex)* tempechoBlockline * outColCount), FreeCUDAHost);
memset(host_echoArr.get(), 0, sizeof(cuComplex) * tempechoBlockline * outColCount);
#pragma omp parallel for
for (long ii = 0; ii < tempechoBlockline * inColCount; ii++) {
host_echoArr.get()[ii] = make_cuComplex(echoArr.get()[ii].real(), echoArr.get()[ii].imag());
for (long ii = 0; ii < tempechoBlockline ; ii++) {
for (long jj = 0; jj < inColCount; jj++) {
host_echoArr.get()[ii* outColCount +jj] = make_cuComplex(echoArr.get()[ii * inColCount + jj].real(), echoArr.get()[ii * inColCount + jj].imag());
}
}
#pragma omp parallel for
for (long ii = 0; ii < tempechoBlockline * outColCount; ii++) {
host_IFFTechoArr.get()[ii] = make_cuComplex(0,0);
host_IFFTechoArr.get()[ii] = make_cuComplex(0, 0);
}
std::shared_ptr<cuComplex> device_echoArr((cuComplex*)mallocCUDADevice(sizeof(cuComplex) * tempechoBlockline * inColCount), FreeCUDADevice);
@ -420,8 +451,11 @@ void TBPImageAlgCls::EchoFreqToTime( )
DeviceToHost(host_IFFTechoArr.get(), device_IFFTechoArr.get(), sizeof(cuComplex) * tempechoBlockline * outColCount);
#pragma omp parallel for
for (long ii = 0; ii < tempechoBlockline * outColCount; ii++) {
IFFTArr.get()[ii] = std::complex<double>(host_IFFTechoArr.get()[ii].x, host_IFFTechoArr.get()[ii].y);
for (long ii = 0; ii < tempechoBlockline ; ii++) {
for (long jj = 0; jj < outColCount; jj++) {
IFFTArr.get()[ii * outColCount + jj] = std::complex<double>(host_IFFTechoArr.get()[ii * outColCount + jj].x,
host_IFFTechoArr.get()[ii * outColCount + jj].y);
}
}
outTimeEchoImg.saveImage(IFFTArr, startechoid, 0, tempechoBlockline, outColCount, 1);