CP1 / COURSE HOME

Week 04 / Describe and decide

Observe a signal over time

Choose when to look, then explain what you actually observed.

Last week we made a decision from one set of inputs. A changing machine gives us a new problem: which moments will our program inspect?

A simulated signal is high from 200 ms up to, but not including, 300 ms. We will inspect it between 0 and 500 ms.

Start with the problem

Try this first.

Draw the high interval on a timeline. Mark samples at 0, 250 and 500 ms, then at 0 and 300 ms. Which schedule notices the event?

Why this week's tool?

A for loop repeats the same observation at selected times. range defines the start, spacing and stopping point, so it also determines what can be missed.

By the end: List the times your program inspected and explain why an empty alarm log does not establish that nothing happened between samples.

The idea behind the program

A loop defines the observations

range(start, stop, step) includes start and excludes stop. range(0, 500, 100) visits 0, 100, 200, 300 and 400. A missing endpoint can change both the sample count and the interpretation of a test.

Timing changes the evidence

Sampling at 0, 250 and 500 ms detects this pulse. Sampling at 0, 300 and 600 ms misses it. Those observations describe two schedules, not two different physical events.

Simulation time is not wall-clock time

Incrementing t_ms in a notebook does not schedule a real sensor at that interval. Here, a loop explores a mathematical timeline. Real acquisition would also involve timing accuracy, latency and sensor behaviour.

A short Python example

Read the example alongside the explanation. Run it in a new notebook cell and change one input to see how it behaves.

hits = 0
for t_ms in range(0, 501, 100):
    high = 200 <= t_ms < 300
    print(t_ms, high)
    if high:
        hits = hits + 1
print('Detected samples:', hits)

Example output

0 False
100 False
200 True
300 False
400 False
500 False
Detected samples: 1

An accumulator remembers the processed part

A loop performs repeated updates. The accumulator has a meaning after every iteration: total is the sum of the values processed so far. That meaning explains both its initial value and where its update belongs.

Draw or trace

Trace the three samples 4, 7 and 2. Draw one row per iteration with columns current sample, previous total and new total.

Predict before running. What does the program remember if total = 0 is moved inside the loop?

Trace and explanation — after your prediction
  1. Before any sample: total = 0, the sum of no values.
  2. After 4, then 7, then 2: total becomes 4, 11, 13.
  3. Resetting inside the loop gives 4, 7, 2 instead: earlier contributions are erased.

The intended answer is 13. A repeated instruction is useful only when its state update preserves the intended meaning.

Change one thing. Replace addition with count = count + 1. Explain why the result becomes 3 rather than 13. For a nested loop, trace one complete inner loop before the outer loop advances.

Türkçe: Toplam değişkeni işlenen kısmın toplamını tutar. Her turda sıfırlamak önceki veriyi siler; saymak ile toplamak farklıdır.

Examples and variations

Each example changes something about the same problem. Open the ones you want to explore and follow the worked explanation.

01100 ms schedule

t = 0, 100, 200, 300, 400, 500 ms

Question: How many high samples? Is 300 ms inside the pulse?

  1. 0, 100 → low
  2. 200 → high
  3. 300, 400, 500 → low

1 high sample out of 6

The pulse excludes 300 ms. Six observations do not reveal the entire continuous signal.

02300 ms schedule

t = 0, 300 ms within the 0–500 ms observation window

Question: Will this schedule detect the pulse?

  1. 0 < 200 → low
  2. 300 is outside [200, 300) → low
  3. No sample lies inside the pulse

0 high samples out of 2

No detection is not proof of no event. The pulse existed between observations.

03Shifted schedule

t = 50, 250, 450 ms

Question: Can fewer samples still detect this particular pulse?

  1. 50 → low
  2. 250 → high
  3. 450 → low

1 high sample out of 3

Phase matters as well as interval. This example does not establish a universal sampling guarantee.

See the Colab code run

Interactive walkthroughs of for Loops & range -- Repetition. Enable JavaScript to step through code, variables, collections and output. The companion notebook remains available below.

Work on it in Colab

Use the notebook to try the ideas yourself. The steps below connect this week's example to the programming practice.

  1. Draw the pulse on a time axis. Mark each sampling time before executing the loop.
  2. Trace the count by hand for 100 ms and 300 ms intervals. Explain why range ends at 501 in the supplied model.
  3. Move the pulse to 210 ≤ t < 290 ms. Predict which of the same schedules now misses it, then adapt the code.
  4. Write two report sentences: one supported by the samples and one tempting claim that the samples cannot support.

Something to take away: A time-axis sketch, two loop traces and a carefully limited conclusion about event detection.

Suggested exercises, downloads & solutions

Read the notebook's teaching cells before these exercises.

  • EX01 Print 1 to 20 (Easy)
  • EX02 Sum 1 to N (Easy)
  • EX05 Count Even Numbers (Medium)
Download notebookWorked solutions

Use the notebook's core and optional labels to choose your workload. This activity fits within guided class time.

Optional notes & guidance
My notes
Using AI or working with a partner

Ask AI whether zero high samples proves the signal stayed low. Supply the pulse and 300 ms schedule as a counterexample. Check its proposed loop endpoints and its distinction between simulated and real time.

You can also review the supplied example with a partner. Use the same inputs to compare the reasoning. Follow the syllabus rules for assessed work.