MATLAB: Plotting the maximum amplitude of p as a function of range.

loopsmatlab functionplotting

Hey guys, I am not sure how plot of the maximum amplitude of p as a function of range.
Could I find the maximum amplitude of p by using peak = max(signal) or do i need another function in matlab?
I am also unsure how I should write it with the loops, since it is my first time working with loops.
Thanks in advance
P1=1; % The sound pressure in Pa at 1 m range
f = 1e3; % The frequency, Hz
w = 2*pi*f; % Angular frequency, radHz
c = 340; % Sound speed, m/s
lambda = c/f; % Wavelength, m
k = 2*pi/lambda; % Wave number, m-1
r = 0.1:0.01:10; % range vector, interspaced by 1 cm
fs = 1e5;
t = [0 : 1/fs : 10/f ]; %time scale, running over ten periods
for i=1:length(t)
p(i,:) = (P1./r).*sin(w*t(i)-k*r);
end
for i=1:length(t)
plot(r,p(i,:));
title([ num2str(t(i)) ' s'])
xlabel('m');
ylabel('Pa')
axis([0 max(r) -10 10])
pause(0.1)
end
for i = 1: length(r)
plot(t',p(:,i));
title([num2str(r(i)) ' m'])
xlabel('s');
ylabel('Pa')
axis([0 max(t) P1*[-10 10] ])
pause;
end

Best Answer

It seems that the first figure does exactly that.
I would also put each loop in a separate figure:
figure
for
...
end
figure
for
...
end
If you simply want to plot the magnitudes of the peaks, the findpeaks (link) function will isolate their amplitudes and distances for you. Use it with the columns of ‘p’. I leave it to you to experiment, since I’m not certain what you’re doing in your code (that appears to me to be both efficient and clever).