Timer anded with a condition ?
Show older comments
hey, i am trying to make an if statement nested in a while loop by having a condition anded with a time period. if the condition is true for that period of time, do what ever is in the if statement.
any ideas ?
2 Comments
Walter Roberson
on 27 Mar 2014
So, for example, "execute this section of code if between 38 and 42 seconds have elapsed since the user pressed the GO button" ?
Or is it "execute this section of code if the time been simulated is between 38 and 42 seconds into the simulated operation" ?
Or closer to, "When you reach a weekend in an hourly time-series, total the income and expenses for the previous week" ?
hassan
on 27 Mar 2014
Answers (1)
Walter Roberson
on 27 Mar 2014
You do not need a timer for that.
You know the number of samples per second, so there is a direct mapping between sample number and time. Record the sample number at which you last detected non-noise, and if you have not reached (sampling frequency * 25) further then do not bother to test.
When conditions are right to test, take the last 25 second's worth of data and search from the end of it towards the front until at least N samples in a row were above the noise floor, where N reflects the shortest continuous sound you consider to be sufficient to keep the system live. You would, for example, not want a single noise sample to be enough to keep the device live. If you do not find such a pattern then time to turn off. If you do find the pattern, then set the last-sound counter to the sample number of the last location you considered to be significant enough to keep the device live.
if CurrentSampleNum >= LastSoundSampleNum + FS * 25
... time to check
end
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!