How to get time of first and second rising edge for the period(simulink)?
5 次查看(过去 30 天)
显示 更早的评论
By the way, I have to calculate the speed of the signal I get from the Hall effect encoder. I decided to use the fronts so in real time I get the current montan front and the previous one to do the delta and I will have the period continuously. Now I can't synchronize the times so as not to have infinity or a nan at the exit. I've tried everything I know. Please help.6
0 个评论
回答(1 个)
Satwik
2024-8-30
Hi,
I understand you want to calculate the speed of the signal obtained from a Hall effect encoder using the rising edges. However, you are encountering issues with synchronizing the timestamps, which is leading to infinite or NaN values in your output. Here is an example that can help you achieve this objective and address the synchronization challenge:
% Example setup
fs = 1000; % Sampling frequency (Hz)
t = 0:1/fs:10; % Time vector (10 seconds)
% Example: Generate a random signal
signal = randn(size(t)); % Replace this with your actual signal
timeVector = t; % Use the time vector corresponding to your signal
% Initialize variables
previousTimestamp = NaN;
currentTimestamp = NaN;
speed = 0;
% Process the signal to detect rising edges and calculate speed[AA2] [SM3]
for i = 2:length(signal)
% Detect rising edge
if signal(i) > signal(i-1)
% Update timestamps
previousTimestamp = currentTimestamp;
currentTimestamp = timeVector(i);
% Calculate time difference
if ~isnan(previousTimestamp)
timeDifference = currentTimestamp - previousTimestamp;
% Avoid division by zero
if timeDifference > 0
% Calculate speed (example calculation)
speed = 1 / timeDifference; % Adjust the formula based on your application
else
speed = NaN; % Handle zero or negative time differences
end
end
end
% Output the speed value
disp(['Speed at time ', num2str(timeVector(i)), ': ', num2str(speed)]);
end
This example simulates a signal from a Hall effect encoder using a square wave. It detects rising edges by checking when the current signal value is greater than the previous value. When a rising edge is detected, timestamps are updated and the time difference between consecutive rising edges is calculated. The speed is calculated as the inverse of this time difference.
Hope this helps!
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!