Plot functions in a for cycle

7 visualizzazioni (ultimi 30 giorni)
Miguel Albuquerque
Miguel Albuquerque il 2 Lug 2022
Commentato: Voss il 2 Lug 2022
Hey guys, thanks in advance,
I have this code , that calculates ambiguity function for signals with a lot of values. On each iteration, I want to plot the graphic with the code below ( cross-correlation). Is there a better way, than for each iteration, interrupt for loop to see the plot?
for k = 1 : 500000 : length(Reference_signal)
for kk = 1: 500000 : length(Surveillance_signal)
Reference_signal_samples = Reference_signal(k : k + 499999);
Surveillance_signal_samples= Surveillance_signal(kk : kk + 499999)
[afmag3,delay3] = ambgfun(Reference_signal_samples,Surveillance_signal_samples,Fs,[250000 250000],'cut','Doppler');
afmag3 = afmag3*1; % Select plot gain *1
afmag3(afmag3>1 )= 1;
end
end
% Plot cross-correlation
[pks3,index3] = max(afmag3);
xMax3 = delay3(index3);
yMax3 = pks3;
subplot(3,2,3)
plot(delay3,afmag3,'LineStyle','-');
hold on
%plot3(delay3(pks3,index3), afmag3(pks3,index3), pks3, '^r');
textString3 = sprintf('(%f, %f)', xMax3, yMax3);
text(xMax3, yMax3,textString3,"Color",'b','FontSize',10);
hold off
shading interp;
%xlim([-0.5e-5 16e-5]);
xlim auto;
grid on;
colorbar;
xlabel('Delay \tau (s)');
ylabel('Ambiguity Function Magnitude');
title('Cross-correlation');

Risposta accettata

Voss
Voss il 2 Lug 2022
You can move your plotting code inside the loops if you want to see a plot in each iteration, yes.
I see that you're doing subplot(3,2,3), so I take that to mean that one of the loops iterates 3 times and the other iterates 2 times, and that you would like to see the plots arranged in a 3-by-2 subplot grid.
If that's the case, something like this would work (actually, this will work regardless of how many iterations each loop has, as long as the idea is to plot the result of each ambgfun call in its own plot):
pulse_size = 500000;
% random data:
Reference_signal = rand(1,2*pulse_size);
Surveillance_signal = rand(1,3*pulse_size);
Fs = 1e6;
% number of blocks of 500000 samples, for each signal:
nRef = numel(Reference_signal)/pulse_size;
nSurv = numel(Surveillance_signal)/pulse_size;
% index of "middle" axes vertically, for ylabel
% (you can use tiledlayout rather than subplot
% to have an actual common ylabel):
if mod(nSurv,2) == 1
nSurvHalf = (nSurv-1)/2+1;
else
nSurvHalf = nSurv/2;
end
% it's convenient to loop over natural numbers
for k = 1:nRef
% Reference_signal_samples doesn't depend on kk, so get it once per k,
% here, before the kk loop:
Reference_signal_samples = Reference_signal((k-1)*pulse_size+(1:pulse_size));
for kk = 1:nSurv
Surveillance_signal_samples = Surveillance_signal((kk-1)*pulse_size+(1:pulse_size));
[afmag3,delay3] = ambgfun(Reference_signal_samples,Surveillance_signal_samples,Fs,[1 1]*pulse_size/2,'cut','Doppler');
% afmag3 = afmag3*1; % Select plot gain *1
afmag3(afmag3 > 1) = 1;
% Plot cross-correlation
[pks3,index3] = max(afmag3);
xMax3 = delay3(index3);
yMax3 = pks3;
subplot(nSurv,nRef,nRef*(kk-1)+k);
plot(delay3,afmag3,'LineStyle','-');
textString3 = sprintf('(%f, %f)', xMax3, yMax3);
text(xMax3, yMax3,textString3,"Color",'b','FontSize',10);
grid on;
% xlabel the bottom-most plots only
if kk == nSurv
xlabel('Delay \tau (s)');
end
% ylabel the plot on the left which is halfway down
if k == 1 && kk == nSurvHalf
ylabel('Ambiguity Function Magnitude');
end
% title each plot
title(sprintf('Ref. pulse: %d Surv. pulse: %d',k,kk));
end
end
% common title for all plots
sgtitle('Cross-correlation');
Try it on your data and see if it produces something like what you have in mind.
  6 Commenti
Miguel Albuquerque
Miguel Albuquerque il 2 Lug 2022
And one more thing, when I run it, I dont see the plots, I dont know what is happening
Voss
Voss il 2 Lug 2022
If each signal is 50 million samples, then using blocks of 500000 samples at a time would give you 10000 plots, arranged in a 100-by-100 grid, so it would be difficult to see anything.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Applications in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by