필터 지우기
필터 지우기

How could I use if function to update the newest maximum value

조회 수: 1 (최근 30일)
Chen Kevin
Chen Kevin 2020년 4월 17일
댓글: Ameer Hamza 2020년 4월 23일
Hi all,
I am going to use photodiode to monitor my laser and capture the max power during the machine is operating.
So, I write the code to generate similar signal and want to build a tool for me can always update the maximum value in the real time.
I have thought about using "if" to update the max value when the latest one is higher but not sure how to implemente it?
Here is my code below, I used rand() to produce random amplitude signal:
fs = 50000; % 50 kHz frequency
Ts = 1/fs*10^9; % sample rate in neno seconds
t = 1:Ts;
pulse = t<=5;
rand_amp = rand(10,1);
sig = pulse.*rand_amp;
sig = reshape(sig', [], 1);
t_total = 1:numel(sig);
plot(t_total, sig);
%find the maximum
maxPulse= max(sig);
xlabel('Time (ns)');
ylabel('Amplitude');

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 4월 17일
Try this
fs = 50000; % 50 kHz frequency
Ts = 1/fs*10^9; % sample rate in neno seconds
t = 1:Ts;
pulse = t<=5;
rand_amp = rand(10,1);
sig = pulse.*rand_amp;
sig = reshape(sig', [], 1);
max_val = zeros(size(sig)); % save maximum value at each time step;
max_val(1) = sig(1); % first maximum value is the first sample of sig
for i=2:numel(max_val)
if sig(i) > max_val(i-1)
max_val(i) = sig(i);
else
max_val(i) = max_val(i-1);
end
end
t_total = 1:numel(sig);
figure;
plot(t_total, sig);
xlabel('Time (ns)');
ylabel('Amplitude');
figure;
plot(t_total, max_val);
xlabel('Time (ns)');
ylabel('Amplitude');
  댓글 수: 19
Chen Kevin
Chen Kevin 2020년 4월 23일
I think I finally got what I want
amp1= [amp].';
fac= amp1./max_val;
It generate a 2000000x10 array, if I am only interest to the final value set, how could I extract them?
Ameer Hamza
Ameer Hamza 2020년 4월 23일
What do you mean by "final value set"? I am not sure how to use this matrix.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matched Filter and Ambiguity Function에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by