最大对比度法自聚焦
function fdr = AutoFocusing_Contrast(InputRealFile, InputImagFile, Range, Azimuth, FocusDepthPoint, INITIAL_STEP, MAX_ITERATIVE_NUM, FINAL_STEP, fdr0,f_prf)
%----------------------------最大對比度法自聚焦-----------------------------------%
% 輸入:距離壓縮后轉角存儲的I/Q兩路數據文件
% 輸出:按聚焦深度估計出的多普勒調頻率
% 算法:最大對比度法
% 準則:選取每個聚焦深度內對比度最大的距離門為參考信號,搜索使該距離門信號成像結果聚焦比最大的fdr作為估計結果
% 調用本模塊需要設定的參數詳見源代碼
% tic;
ta = (0 : Azimuth-1)*(1/f_prf) ;
T_max = Azimuth*(1/f_prf);
%--------------------------------進行方位壓縮,選取參考信號-------------------------------%
Ndepth = ceil(Range / FocusDepthPoint); ? ? ? ? ?% 測繪帶內的聚焦深度數
% 打開距離壓縮后轉角存儲的I/Q兩路數據文件
FidReadReal = fopen(InputRealFile,'r');
FidReadImag = fopen(InputImagFile,'r');
if((Range - FocusDepthPoint * Ndepth) ~= 0)
? ? temp_shy = 1;
else
? ? temp_shy = 0;
end
max_value = zeros(1,Ndepth);
max_pos = zeros(1,Ndepth);
for i = 1 : Ndepth ? ?
? ? AzimuthRef = conj(exp(j*pi*fdr0(i)* ( ta -T_max/2 ).^2 ));
? ? if((temp_shy == 1) && (i == Ndepth))
? ? ? ? m_stop = Range - FocusDepthPoint * (Ndepth - 1);
? ? else
? ? ? ? m_stop = FocusDepthPoint;
? ? end
? ? focus_rate = zeros(1,m_stop); ? ?
? ? % 取每個聚焦深度內聚焦比最大的距離門作為參考信號
? ? for m = 1 : m_stop
? ? ? ? TempRe = fread(FidReadReal, Azimuth, 'float32');
? ? ? ? TempIm = fread(FidReadImag, Azimuth, 'float32');
? ? ? ? Temp = (TempRe + j*TempIm).';
? ? ? ? Hout = Temp .* AzimuthRef;
? ? ? ? out_effective = abs(fft(Hout)); ? ? ? ?
? ? ? ? % 計算聚焦比
? ? ? ? focus_rate(m) = var(out_effective) / mean(out_effective);
? ?end ? ?
? ? [max_value(i), max_pos(i)] = max(focus_rate);
end
%-----------------------------------最大對比度法自聚焦-----------------------------------%
first_validfdr_pos = -1; ? ? ? ?% 存放第一個自聚焦成功的聚焦深度,是測繪帶內的第幾個聚焦深度
update_step = 0; ? ? ? ? ? ? ? ?% 清除步長更新標識
fdr_step = INITIAL_STEP; ? ? ? ?% 設定自聚焦估計的初始搜索步長;注意:fdr的步長是在初始步長的基礎上,每次減小一倍,直到步長不大于1Hz;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? % 因此,如果初始步長是2的整數次冪,則fdr的估計精度為1Hz
iterative_num = 1; ? ? ? ? ? ? ?% 初始化迭代次數標識
autofocusing_over = 0; ? ? ? ? ?% 初始化自聚焦結束表示
autofocusing_fail = 0; ? ? ? ? ?% 初始化自聚焦失敗標識
fdr = zeros(1, Ndepth);
focus_rate_current_fdr = zeros(1,3);
for i = 1 : Ndepth
? ? % 當前聚焦深度的fdr初值
? ? current_fdr = fdr0(i);
? ? % 讀入當前聚焦深度的參考信號
? ? fseek(FidReadReal, ((i - 1) * FocusDepthPoint + max_pos(i) - 1) * Azimuth * 4, -1);
? ? fseek(FidReadImag, ((i - 1) * FocusDepthPoint + max_pos(i) - 1) * Azimuth * 4, -1);
? ? % 得到參考信號頻譜
? ? TempRe = fread(FidReadReal, Azimuth, 'float32');
? ? TempIm = fread(FidReadImag, Azimuth, 'float32');
? ? Temp = (TempRe + j*TempIm).';
% % % % % ? ? HTemp = fft(Temp);
? ??
? ? % 反復迭代,直到估計出當前聚焦深度的fdr
? ? while(autofocusing_over ~= 1)
? ? ? ? % 反復迭代,直到在當前搜索步長下估計出fdr
? ? ? ? while(update_step ~= 1)
? ? ? ? ? ? % 判斷當前搜索步長是否為初始步長
? ? ? ? ? ? if(fdr_step == INITIAL_STEP)
? ? ? ? ? ? ? ? % 判斷是否為基于初始步長的第一次迭代
? ? ? ? ? ? ? ? % 是,則需要計算三個聚焦比,即:current_fdr-fdr_step、current_fdr、current_fdr+fdr_step
? ? ? ? ? ? ? ? if(iterative_num == 1)
? ? ? ? ? ? ? ? ? ? leftpos_fdr ?= -1;
? ? ? ? ? ? ? ? ? ? rightpos_fdr = ?1;
? ? ? ? ? ? ? ? ? ? m_step = 1;
? ? ? ? ? ? ? ? % 否,則需要計算一個聚焦比,即:current_fdr-fdr_step或current_fdr+fdr_step
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? leftpos_fdr ?= current_peak - 2;
? ? ? ? ? ? ? ? ? ? rightpos_fdr = current_peak - 2;
? ? ? ? ? ? ? ? ? ? m_step = 1;
? ? ? ? ? ? ? ? end
? ? ? ? ? ? % 不是初始步長則不需要迭代,而且需要計算兩個聚焦比,即:current_fdr-fdr_step、current_fdr+fdr_step
? ? ? ? ? ? else
?? ??? ??? ??? ??? ?leftpos_fdr ?= -1;
?? ??? ??? ??? ??? ?rightpos_fdr = ?1;
?? ??? ??? ??? ??? ?m_step = 2;
? ? ? ? ? ? end?? ??? ? ? ?
? ? ? ? ? ? for m = leftpos_fdr : m_step : rightpos_fdr
? ? ? ? ? ? ? ? % 利用上一次迭代結果,更新方位向參考函數 ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? AzimuthRef = conj(rectpuls( ta -T_max/2 ,T_max).*exp(j*pi*(current_fdr + m*fdr_step)* ( ta - T_max/2 ).^2 ));
? ? ? ? ? ? ? ? Hout = Temp .* AzimuthRef;
? ? ? ? ? ? ? ? out_effective = abs(fft(Hout));
? ? ? ? ? ? ? ? % 計算聚焦比 ???????為什么不是標準差比均值了
? ? ? ? ? ? ? ? mean_value = mean(out_effective);
? ? ? ? ? ? ? ? [max1, pos1] = max(out_effective);
? ? ? ? ? ? ? ? out_effective(pos1) = 0;
? ? ? ? ? ? ? ? [max2, pos2] = max(out_effective);
? ? ? ? ? ? ? ? out_effective(pos2) = 0;
? ? ? ? ? ? ? ? [max3, pos3] = max(out_effective);
? ? ? ? ? ? ? ? max_value = (max1 + max2 + max3) / 3;
? ? ? ? ? ? ? ? focus_rate_current_fdr(m + 2) = max_value / mean_value;
? ? ? ? ? ? end
? ? ? ? ? ? % 尋找相鄰三個fdr中,所成圖像聚焦比最大的那個fdr
? ? ? ? ? ? [current_max_value, current_peak] = max(focus_rate_current_fdr);
? ? ? ? ? ??
? ? ? ? ? ? % 判斷是否中間的fdr所成的圖像聚焦比最大
? ? ? ? ? ? if(current_peak == 2)
? ? ? ? ? ? ? ? % 判斷當前步長是否足夠小,是的話結束當前聚焦深度的自聚焦
? ? ? ? ? ? ? ? if(fdr_step <= FINAL_STEP)
? ? ? ? ? ? ? ? ? ? update_step = 1;
? ? ? ? ? ? ? ? ? ? autofocusing_over = 1;
? ? ? ? ? ? ? ? % 否則更新步長繼續進行自聚焦
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? update_step = 1;
? ? ? ? ? ? ? ? ? ? fdr_step = fdr_step / 2;?? ??? ??? ??? ??? ??? ?
? ? ? ? ? ? ? ? end
? ? ? ? ? ? % 如果是邊上的fdr所成的圖像聚焦比最大,則要做兩件事情
? ? ? ? ? ? else
? ? ? ? ? ? ? ? % 1、判斷當前步長是否為初始步長,以決定是否需要更新步長
? ? ? ? ? ? ? ? if fdr_step == INITIAL_STEP
? ? ? ? ? ? ? ? ? ? iterative_num = iterative_num + 1;
? ? ? ? ? ? ? ? ? ? current_fdr = current_fdr + (current_peak - 2) * fdr_step;
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? current_fdr = current_fdr + (current_peak - 2) * fdr_step;
? ? ? ? ? ? ? ? ? ? fdr_step = fdr_step / 2;
? ? ? ? ? ? ? ? ? ? update_step = 1;
? ? ? ? ? ? ? ? end ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? % 2、更新存放聚焦比的數組focus_rate_current_fdr
? ? ? ? ? ? ? ? if(current_peak == 1)
? ? ? ? ? ? ? ? ? ? focus_rate_current_fdr(3) = focus_rate_current_fdr(2);
? ? ? ? ? ? ? ? ? ? focus_rate_current_fdr(2) = focus_rate_current_fdr(1);
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? focus_rate_current_fdr(1) = focus_rate_current_fdr(2);
? ? ? ? ? ? ? ? ? ? focus_rate_current_fdr(2) = focus_rate_current_fdr(3);
? ? ? ? ? ? ? ? end
? ? ? ? ? ? end ? ? ? ? ? ?
? ? ? ? ? ? % 在初始搜索步長下,如果迭代次數超出允許的最大次數,則當前聚焦深度的自聚焦處理失敗
? ? ? ? ? ? if(iterative_num > MAX_ITERATIVE_NUM)
? ? ? ? ? ? ? ? update_step = 1;
? ? ? ? ? ? ? ? autofocusing_over = 1;
? ? ? ? ? ? ? ? autofocusing_fail = 1;?? ??? ??? ??? ??? ?
? ? ? ? ? ? end
? ? ? ? end % end of while(update_step ~= 1), ? ? ? ?
? ? ? ? % 清除步長更新標識
? ? ? ? update_step = 0; ? ? ? ?
? ? end % end of while(autofocusing_over ~= 1),
? ??
? ? % 迭代步長恢復初始搜索步長
? ? fdr_step = INITIAL_STEP;
? ? % 迭代次數歸一
? ? iterative_num = 1;
? ??
? ? % 存儲估得的當前聚焦深度的fdr值
?? ?if autofocusing_fail == 0
? ? ? ? fdr(i) = current_fdr;
? ? ? ? % 記錄第一個自聚焦成功的聚焦深度的位置
? ? ? ? if(first_validfdr_pos == -1)
? ? ? ? ? ? first_validfdr_pos = i;
? ? ? ? end
? ? else
? ? ? ? fdr(i) = 12345678;
? ? end ? ?
? ? % 清除自聚焦結束標識
? ? autofocusing_over = 0;
? ? % 清除自聚焦失敗標識
? ? autofocusing_fail = 0;
end % end of for i = 1 : Ndepth
% 判斷是否所有聚焦深度的自聚焦都失敗
if(first_validfdr_pos == -1)
? ? autofocusing_fail_total = 1;
% 不是徹底失敗的話,令第一個聚焦深度的fdr值等于第一個自聚焦成功的聚焦深度的fdr值
else
? ? autofocusing_fail_total = 0;
?? ?fdr(1) = fdr(first_validfdr_pos);
end
% 計算自聚焦失敗的聚焦深度的fdr值,兩種方法:
% 1、如果后一個聚焦深度自聚焦成功,取前后兩個聚焦深度的fdr值的平均;
% 2、否則令當前聚焦深度的fdr值等于前一個聚焦深度的fdr值
if(autofocusing_fail_total ~= 1)
? ? for i = 2 : 1 : Ndepth
? ? ? ? if(fdr(i) == 12345678)
? ? ? ? ? ? if(i ~= Ndepth)
? ? ? ? ? ? ? ? if(fdr(i + 1) ~= 12345678)
? ? ? ? ? ? ? ? ? ? fdr(i) = (fdr(i - 1) + fdr(i + 1)) / 2;
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? fdr(i) = fdr(i - 1);
? ? ? ? ? ? ? ? end
? ? ? ? ? ? else
? ? ? ? ? ? ? ? fdr(i) = fdr(i - 1);
? ? ? ? ? ? end
? ? ? ? end
? ? end
end
fclose(FidReadReal);
fclose(FidReadImag);
?
總結
- 上一篇: 基于FPGA的波速形成实现
- 下一篇: 阵元间隔为半波长的均匀分布16元线阵