diff --git a/codegen_fracFdpw/TBc_fracFdpwLFM.m b/codegen_fracFdpw/TBc_fracFdpwLFM.m index 8916a6d..efab983 100644 --- a/codegen_fracFdpw/TBc_fracFdpwLFM.m +++ b/codegen_fracFdpw/TBc_fracFdpwLFM.m @@ -19,19 +19,19 @@ % - FrFT is computed on the interpolated signal. % - Power spectra are averaged across the entire DPW. -clear +clearvars -except out clc close all %% Signal parameters N = 512; -Nframes = 64; +Nframes = 1024; Fs = single(512e6); T = single(1e-6); -B = single(64e6); +B = single(32e6); beta = B/T; %% Time axis @@ -146,4 +146,134 @@ ylabel('Power (dB)') fprintf('\n'); fprintf('FFT peak bin : %d\n',idxFFT); fprintf('FrFT peak bin : %d\n',idxFrFT); -fprintf('\n'); \ No newline at end of file +fprintf('\n'); + +%% Compare TBc against TBm (optional) +% +% If the Simulink model has been executed and produced out.Fsim, +% compare both implementations. + +if exist('out','var') + + fprintf('\n'); + fprintf('TBc vs TBm Comparison\n'); + fprintf('---------------------\n'); + + Ftbm = out.Fsim; + + %% Dimension check + + fprintf('TBc size : [%d %d]\n', ... + size(Ffrft,1), size(Ffrft,2)); + + fprintf('TBm size : [%d %d]\n', ... + size(Ftbm,1), size(Ftbm,2)); + + assert(isequal(size(Ffrft),size(Ftbm)), ... + 'TBc and TBm dimensions differ.'); + + %% Error metrics + + err = Ftbm - Ffrft; + + maxErr = max(abs(err(:))); + + rmsErr = sqrt(mean(abs(err(:)).^2)); + + refPeak = max(abs(Ffrft(:))); + + relErr = maxErr / refPeak; + + %% Results + + fprintf('\n'); + fprintf('Reference peak : %.9g\n',double(refPeak)); + fprintf('Maximum error : %.9g\n',double(maxErr)); + fprintf('RMS error : %.9g\n',double(rmsErr)); + fprintf('Relative error : %.9g\n',double(relErr)); + + if maxErr == 0 + + fprintf('\nPASS: Outputs are bit-identical.\n'); + + elseif relErr < 1e-5 + + fprintf('\nPASS: Outputs are numerically equivalent.\n'); + + else + + fprintf('\nWARNING: Outputs differ.\n'); + + end + + %% Visual comparison + + frameIdx = 1; + + figure + + subplot(3,1,1) + + plot(abs(Ffrft(:,frameIdx))) + + grid on + + title('TBc Output') + xlabel('Bin') + ylabel('|F|') + + subplot(3,1,2) + + plot(abs(Ftbm(:,frameIdx))) + + grid on + + title('TBm Output') + xlabel('Bin') + ylabel('|F|') + + subplot(3,1,3) + + plot(abs(Ftbm(:,frameIdx) - Ffrft(:,frameIdx))) + + grid on + + title('Absolute Error') + xlabel('Bin') + ylabel('|Error|') + + %% Mean power spectrum comparison + + Ptbc = mean(abs(Ffrft).^2,2); + + Ptbm = mean(abs(Ftbm).^2,2); + + Ptbc_dB = 10*log10(Ptbc/max(Ptbc)); + + Ptbm_dB = 10*log10(Ptbm/max(Ptbm)); + + figure + + plot(Ptbc_dB,'LineWidth',1.5) + + hold on + + plot(Ptbm_dB,'--','LineWidth',1.5) + + grid on + + ylim([-60 5]) + + xlabel('Bin') + ylabel('Power (dB)') + + title('TBc vs TBm Mean Power Spectrum') + + legend('TBc','TBm') + +else + + fprintf('\n'); + fprintf('TBm comparison skipped (out.Fsim not found).\n'); + +end \ No newline at end of file diff --git a/codegen_fracFdpw/TBm_fracFdpw.slx b/codegen_fracFdpw/TBm_fracFdpw.slx index 6c8435b..91cd7d0 100644 Binary files a/codegen_fracFdpw/TBm_fracFdpw.slx and b/codegen_fracFdpw/TBm_fracFdpw.slx differ