增加matlab 转义代码

pull/6/head
chenzenghui 2025-03-03 16:25:50 +08:00
parent bc2034b3a9
commit 16b8abb6c5
8 changed files with 330 additions and 40 deletions

View File

@ -0,0 +1,172 @@
#include <cstdio>
#include <cufft.h>
#include <cmath>
#include <cuda_runtime.h>
#include <iostream>
#include <memory>
#include <cmath>
#include <complex>
#include <device_launch_parameters.h>
#include <cuda_runtime.h>
#include <cublas_v2.h>
#include <cuComplex.h>
#include "BaseConstVariable.h"
#include "GPUTool.cuh"
#include "GPUBPTool.cuh"
#include "BPBasic0_CUDA.cuh"
__global__ void phaseCompensationKernel(cufftComplex* phdata, const float* Freq, float r, int K, int Na) {
int freqIdx = blockIdx.x * blockDim.x + threadIdx.x;
int pulseIdx = blockIdx.y * blockDim.y + threadIdx.y;
if (freqIdx >= K || pulseIdx >= Na) return;
int idx = pulseIdx * K + freqIdx;
float phase = 4 * PI * Freq[freqIdx] * r / c;
float cos_phase = cosf(phase);
float sin_phase = sinf(phase);
cufftComplex ph = phdata[idx];
float new_real = ph.x * cos_phase - ph.y * sin_phase;
float new_imag = ph.x * sin_phase + ph.y * cos_phase;
phdata[idx] = make_cuComplex(new_real, new_imag);
}
__global__ void fftshiftKernel(cufftComplex* data, int Nfft, int Np) {
int pulse = blockIdx.x * blockDim.x + threadIdx.x;
if (pulse >= Np) return;
int half = Nfft / 2;
for (int i = 0; i < half; ++i) {
cufftComplex temp = data[pulse * Nfft + i];
data[pulse * Nfft + i] = data[pulse * Nfft + i + half];
data[pulse * Nfft + i + half] = temp;
}
}
__global__ void processPulseKernel(int nx, int ny, const float* x_mat, const float* y_mat, const float* z_mat,
float AntX, float AntY, float AntZ, float R0, float minF,
const cufftComplex* rc_pulse, float r_start, float dr, int nR,
cufftComplex* im_final) {
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x >= nx || y >= ny) return;
int idx = x * ny + y;
float dx = AntX - x_mat[idx];
float dy = AntY - y_mat[idx];
float dz = AntZ - z_mat[idx];
float dR = sqrtf(dx * dx + dy * dy + dz * dz) - R0;
// Range check
if (dR < r_start || dR >= (r_start + dr * (nR - 1))) return;
// Linear interpolation
float pos = (dR - r_start) / dr;
int index = (int)floorf(pos);
float weight = pos - index;
if (index < 0 || index >= nR - 1) return;
cufftComplex rc_low = rc_pulse[index];
cufftComplex rc_high = rc_pulse[index + 1];
cufftComplex rc_interp;
rc_interp.x = rc_low.x * (1 - weight) + rc_high.x * weight;
rc_interp.y = rc_low.y * (1 - weight) + rc_high.y * weight;
// Phase correction
float phase = 4 * PI * minF * dR / c;
float cos_phase = cosf(phase);
float sin_phase = sinf(phase);
cufftComplex phCorr;
phCorr.x = rc_interp.x * cos_phase - rc_interp.y * sin_phase;
phCorr.y = rc_interp.x * sin_phase + rc_interp.y * cos_phase;
// Accumulate
im_final[idx].x += phCorr.x;
im_final[idx].y += phCorr.y;
}
void bpBasic0CUDA(GPUDATA& data, int flag) {
// Phase compensation
if (flag == 1) {
dim3 block(16, 16);
dim3 grid((data.K + 15) / 16, (data.Np + 15) / 16);
phaseCompensationKernel << <grid, block >> > (data.phdata, data.Freq, data.R0, data.K, data.Np);
cudaCheckError(cudaDeviceSynchronize());
data.R0 = data.r; // 假设data.r已正确设置
}
// FFT处理
cufftHandle plan;
cufftPlan1d(&plan, data.Nfft, CUFFT_C2C, data.Np);
cufftExecC2C(plan, data.phdata, data.phdata, CUFFT_INVERSE);
cufftDestroy(plan);
// FFT移位
dim3 blockShift(256);
dim3 gridShift((data.Np + 255) / 256);
fftshiftKernel << <gridShift, blockShift >> > (data.phdata, data.Nfft, data.Np);
cudaCheckError(cudaDeviceSynchronize());
// 图像重建
float r_start = data.r_vec[0];
float dr = (data.r_vec[data.Nfft - 1] - r_start) / (data.Nfft - 1);
dim3 block(16, 16);
dim3 grid((data.nx + 15) / 16, (data.ny + 15) / 16);
for (int ii = 0; ii < data.Np; ++ii) {
processPulseKernel << <grid, block >> > (
data.nx, data.ny,
data.x_mat, data.y_mat, data.z_mat,
data.AntX[ii], data.AntY[ii], data.AntZ[ii],
data.R0, data.minF[ii],
data.phdata + ii * data.Nfft,
r_start, dr, data.Nfft,
data.im_final
);
cudaCheckError(cudaPeekAtLastError());
}
cudaCheckError(cudaDeviceSynchronize());
}
void initGPUData(GPUDATA& h_data, GPUDATA& d_data) {
}
void freeGPUData(GPUDATA& d_data) {
}
//int main() {
// GPUDATA h_data, d_data;
//
// // 初始化主机数据
// h_data.Nfft = 1024;
// h_data.K = 512;
// // ... 其他参数初始化
//
// // 初始化设备内存
// initGPUData(h_data, d_data);
//
// // 执行算法
// bpBasic0CUDA(d_data, 0);
//
// // 拷贝结果回主机
// cudaCheckError(cudaMemcpy(h_data.im_final, d_data.im_final,
// sizeof(cufftComplex) * h_data.nx * h_data.ny, cudaMemcpyDeviceToHost));
//
// // 释放资源
// freeGPUData(d_data);
//
// return 0;
//}

View File

@ -0,0 +1,32 @@
#include <cstdio>
#include <cufft.h>
#include <cmath>
#include <cuda_runtime.h>
#include "BaseConstVariable.h"
#define cudaCheckError(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char* file, int line) {
if (code != cudaSuccess) {
fprintf(stderr, "CUDA Error: %s %s %d\n", cudaGetErrorString(code), file, line);
exit(code);
}
}
#define c LIGHTSPEED
struct GPUDATA {
int Nfft, K, Np, nx, ny; // 傅里叶点数、频点数、脉冲数、图像列、图像行
float* AntX, * AntY, * AntZ, * minF; // 天线坐标、起始频率
float* x_mat, * y_mat, * z_mat;// 地面坐标
float* r_vec; // 坐标范围
cufftComplex* phdata;// 回波
cufftComplex* im_final;// 图像
float R0; // 参考斜距
};
extern "C" {
void initGPUData(GPUDATA& h_data, GPUDATA& d_data);
void freeGPUData(GPUDATA& d_data);
};

View File

@ -173,6 +173,12 @@ __global__ void kernel_TimeBPImageGridNet(double* antPx, double* antPy, double*
__device__ double computerR(double& Px, double& Py, double& Pz, double& Tx, double& Ty, double& Tz) {
//double R= sqrt((Px - Tx) * (Px - Tx) + (Py - Ty) * (Py - Ty) + (Pz - Tz) * (Pz - Tz));
//if (R > 900000) {
// printf("R=%f\n", R);
//}
//return R;
return sqrt((Px - Tx) * (Px - Tx) + (Py - Ty) * (Py - Ty) + (Pz - Tz) * (Pz - Tz));
}
@ -198,17 +204,24 @@ __device__ void updateBPImage(
cuComplex s0 = TimeEchoArr[pid0];
cuComplex s1 = TimeEchoArr[pid1];
if (isinf(s0.x) || isinf(s0.y) || isinf(s1.x) || isinf(s1.y)) {
return;
}
cuComplex s = make_cuComplex(
s0.x + (s1.x - s0.x) * (ridx-Ridx0), // real
s0.y + (s1.y - s0.y) * (ridx-Ridx0) // imag
);
double phi = 4 * LIGHTSPEED / startLamda * (R - RefRange);
double phi = 4 * PI / startLamda * (R - RefRange);
// exp(ix)=cos(x)+isin(x)
cuComplex phiCorr = make_cuComplex(cos(phi), sin(phi));
s = cuCmulf(s, phiCorr); // 校正后
imgArr[pixelidx] = cuCaddf(imgArr[pixelidx], s);
//imgArr[pixelidx] = cuCaddf(imgArr[pixelidx], make_cuComplex(1,1));
return;
}
@ -231,12 +244,14 @@ __device__ void segmentBPImage(
long currentprfid = 0;
// 0
currentprfid = currentprfid + 0;
currentprfid = startSegmentPrfId + 0;
double Px = antPx[currentprfid];
double Py = antPy[currentprfid];
double Pz = antPz[currentprfid];
double R0 = computerR(Px, Py, Pz, Tx, Ty, Tz);
if (Rnear <= R0 && R0 <= RRrange) {
if (LRrange <= R0 && R0 <= RRrange) {
updateBPImage(
currentprfid, pixelID, R0, LRrange,
TimeEchoArr, prfcount, pointCount,
@ -246,7 +261,7 @@ __device__ void segmentBPImage(
}
// 10
currentprfid = currentprfid + 10;
currentprfid = startSegmentPrfId + 10;
Px = antPx[currentprfid];
Py = antPy[currentprfid];
Pz = antPz[currentprfid];
@ -262,7 +277,7 @@ __device__ void segmentBPImage(
//19
currentprfid = currentprfid + 19;
currentprfid = startSegmentPrfId + 19;
Px = antPx[currentprfid];
Py = antPy[currentprfid];
Pz = antPz[currentprfid];
@ -308,7 +323,7 @@ __device__ void segmentBPImage(
}
#pragma unroll
for (long i = 11; i < 19; i++) {
for (long i = 11; i < 20; i++) {
currentprfid = startSegmentPrfId + i;
if (currentprfid < prfcount) {
Px = antPx[currentprfid];
@ -343,18 +358,40 @@ __global__ void kernel_pixelTimeBP(
double Tx = imgx[idx]; // 地面坐标点
double Ty = imgy[idx];
double Tz = imgz[idx];
double Rrange = pointCount * dx;// 成像范围
double LRrange = RefRange - Rrange / 2;//左范围
double RRrange = RefRange + Rrange / 2;
for (long segid = 0; segid < prfcount; segid = segid + 20) {
// 判断范围
segmentBPImage(
antPx, antPy, antPz,
Tx, Ty, Tz,
TimeEchoArr, prfcount, pointCount,
imgArr,
startLamda, Rnear, dx, RefRange, Rfar,
segid, idx
);
for (long segid = 0; segid < prfcount; segid = segid + 20) {
long seglen = prfcount - segid;
if (seglen < 20) {
for (long i = 1; i < 10; i++) {
long currentprfid = segid + i;
if (currentprfid < prfcount) {
double Px = antPx[currentprfid];
double Py = antPy[currentprfid];
double Pz = antPz[currentprfid];
double R = computerR(Px, Py, Pz, Tx, Ty, Tz);
updateBPImage(
currentprfid, idx, R, LRrange,
TimeEchoArr, prfcount, pointCount,
imgArr,
startLamda, Rnear, dx, RefRange, Rfar
);
}
}
}
else {
// 判断范围
segmentBPImage(
antPx, antPy, antPz,
Tx, Ty, Tz,
TimeEchoArr, prfcount, pointCount,
imgArr,
startLamda, Rnear, dx, RefRange, Rfar,
segid, idx
);
}
}
}

View File

@ -85,25 +85,15 @@
<property name="text">
<string>图像网格</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit">
<property name="enabled">
<bool>false</bool>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>30</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="GridNetBtn">
<property name="enabled">
<bool>false</bool>
<bool>true</bool>
</property>
<property name="minimumSize">
<size>
@ -112,7 +102,23 @@
</size>
</property>
<property name="text">
<string>PushButton</string>
<string>D:\Programme\vs2022\RasterMergeTest\simulationData\demdataset\demxyz.bin</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="GridNetBtn">
<property name="enabled">
<bool>true</bool>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>30</height>
</size>
</property>
<property name="text">
<string>选择</string>
</property>
</widget>
</item>

View File

@ -215,6 +215,7 @@ ErrorCode TBPImageAlgCls::Process(long num_thread)
qDebug() << u8"创建成像平面的XYZ";
QString outRasterXYZ = JoinPath(this->L1ds->getoutFolderPath(), this->L0ds->getSimulationTaskName() + "_xyz.bin");
CreatePixelXYZ(this->L0ds, outRasterXYZ);
return this->ProcessWithGridNet(num_thread, outRasterXYZ);
}
@ -322,7 +323,7 @@ ErrorCode TBPImageAlgCls::ProcessGPU()
double Px = 0;
double Py = 0;
double Pz = 0;
for (long i = 0; i < rowCount; i++) {
for (long i = 0; i < PRFCount; i++) {
time = antpos.get()[i *19 + 0];
Px = antpos.get()[i *19 + 1];
Py = antpos.get()[i *19 + 2];
@ -394,7 +395,25 @@ ErrorCode TBPImageAlgCls::ProcessGPU()
antpx.get()[anti] = Pxs.get()[anti + startechoid];
antpy.get()[anti] = Pys.get()[anti + startechoid];
antpz.get()[anti] = Pzs.get()[anti + startechoid];
if (abs(antpx.get()[anti]) < 10 || abs(antpy.get()[anti]) < 10 || abs(antpz.get()[anti]) < 10) {
qDebug()<<anti<<":" << antpx.get()[anti] << "," << antpy.get()[anti] << ","<< antpz.get()[anti] ;
}
}
double maxdouble = 0;
for (long i = 0; i < tempechoBlockline; i++) {
for (long j = 0; j < PlusePoints; j++) {
if (echoArr.get()[i * PlusePoints + j].real() > maxdouble) {
maxdouble = echoArr.get()[i * PlusePoints + j].real();
}
if (echoArr.get()[i * PlusePoints + j].imag() > maxdouble) {
maxdouble = echoArr.get()[i * PlusePoints + j].imag();
}
}
}
printf("echo %f\n", maxdouble);
TBPImageGPUAlg2(
@ -407,8 +426,7 @@ ErrorCode TBPImageAlgCls::ProcessGPU()
tempimgBlockline, colCount,
tempechoBlockline, PlusePoints
);
qDebug() << QString(" image block PRF:[%1] \t").arg((startechoid + tempechoBlockline) * 100.0 / PRFCount)
<< startechoid << "\t-\t" << startechoid + tempechoBlockline;
qDebug() << QString(" image block PRF:[%1] \t").arg((startechoid + tempechoBlockline) * 100.0 / PRFCount)<< startechoid << "\t-\t" << startechoid + tempechoBlockline;
}
this->L1ds->saveImageRaster(imgArr, startimgrowid, tempimgBlockline);
}
@ -455,14 +473,24 @@ void TBPImageGPUAlg2(std::shared_ptr<double> antPx, std::shared_ptr<double> antP
std::shared_ptr<cuComplex> h_imgArr((cuComplex*)mallocCUDAHost(sizeof(cuComplex) * rowcount * colcount), FreeCUDAHost);
std::shared_ptr<cuComplex> d_imgArr((cuComplex*)mallocCUDADevice(sizeof(cuComplex) * rowcount * colcount), FreeCUDADevice);
#pragma omp parallel for
//#pragma omp parallel for
double maxdouble = 0;
for (long i = 0; i < prfcount; i++) {
for (long j = 0; j < freqcount; j++) {
h_echoArr.get()[i * freqcount + j] = make_cuComplex(echoArr.get()[i * freqcount + j].real(),
echoArr.get()[i * freqcount + j].imag());
if (h_echoArr.get()[i * freqcount + j].x > maxdouble) {
maxdouble = h_echoArr.get()[i * freqcount + j].x;
}
if (h_echoArr.get()[i * freqcount + j].y > maxdouble) {
maxdouble = h_echoArr.get()[i * freqcount + j].y;
}
}
}
printf("h_echoArr max: %e\n", maxdouble);
// 天线位置
for (long i = 0; i < prfcount; i++) {
h_antPx.get()[i] = antPx.get()[i];
@ -499,7 +527,8 @@ void TBPImageGPUAlg2(std::shared_ptr<double> antPx, std::shared_ptr<double> antP
// 直接使用
double startlamda = LIGHTSPEED / freq;
TimeBPImage(
d_antPx.get(), d_antPy.get(), d_antPz.get(),
d_imgx.get(), d_imgy.get(), d_imgz.get(),
@ -513,14 +542,20 @@ void TBPImageGPUAlg2(std::shared_ptr<double> antPx, std::shared_ptr<double> antP
#pragma omp parallel for
maxdouble = 0;
for (long i = 0; i < rowcount; i++) {
for (long j = 0; j < colcount; j++) {
img_arr.get()[i * colcount + j] = std::complex<double>(h_imgArr.get()[i * colcount + j].x, h_imgArr.get()[i * colcount + j].y);
if (img_arr.get()[i * colcount + j].real() > maxdouble) {
maxdouble = img_arr.get()[i * colcount + j].real();
}
if (img_arr.get()[i * colcount + j].imag() > maxdouble) {
maxdouble = img_arr.get()[i * colcount + j].imag();
}
}
}
printf("echoImage max: %f\n", maxdouble);
}

View File

@ -228,7 +228,9 @@
<QtMoc Include="SimulationSAR\QSARLookTableSimualtionGUI.h" />
<QtMoc Include="SimulationSAR\QSimulationBPImage.h" />
<QtMoc Include="SimulationSAR\QSimulationRFPCGUI.h" />
<CudaCompile Include="SimulationSAR\BPBasic0_CUDA.cu" />
<CudaCompile Include="SimulationSAR\GPUBPTool.cuh" />
<ClInclude Include="SimulationSAR\BPBasic0_CUDA.cuh" />
<ClInclude Include="SimulationSAR\RFPCProcessCls.h" />
<ClInclude Include="SimulationSAR\SARSatelliteSimulationAbstractCls.h" />
<ClInclude Include="SimulationSAR\SARSimulationTaskSetting.h" />

View File

@ -59,6 +59,9 @@
<ClInclude Include="PowerSimulationIncoherent\OribtModelOperator.h">
<Filter>PowerSimulationIncoherent</Filter>
</ClInclude>
<ClInclude Include="SimulationSAR\BPBasic0_CUDA.cuh">
<Filter>SimulationSAR</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="SimulationSAR\QImageSARRFPC.cpp">
@ -178,5 +181,8 @@
<CudaCompile Include="SimulationSAR\GPUBPTool.cuh">
<Filter>SimulationSAR</Filter>
</CudaCompile>
<CudaCompile Include="SimulationSAR\BPBasic0_CUDA.cu">
<Filter>SimulationSAR</Filter>
</CudaCompile>
</ItemGroup>
</Project>