Data representation · GCSE · OCR J277 1.2.4, AQA 8525 3.3.7, Edexcel 1CP2 2.2.3 · about 15 min
Sampling, sample rate, bit depth and file size, and finding a note from its samples.
[1 mark]A sound is sampled at 8,000 Hz with an 8-bit depth for 2 seconds. How many bits is it?
[1 mark]What is the sample rate?
[1 mark]What does increasing the bit depth do?
[1 mark]A 440 Hz wave crosses the middle going upwards how many times in half a second?
[1 mark]How many levels can a 2-bit sample take?
Sample a note of 659 Hz for half a second at a sample rate of 8,000 Hz. Print samples: <n> and 8-bit size: <n> bits, worked out from the rate and duration. Then count the upward crossings, print note: <freq> Hz, and play that frequency on the buzzer for half a second.
# the two lines every program starts with: the commands, then the robot from bugbot import * connect() import math rate = 8000 seconds = 0.5
The hint students can ask for: Build the samples with a sine wave, then find the frequency by counting how many times the wave crosses zero on the way up. Divide those crossings by how long the recording lasted.
from bugbot import *
connect()
import math
rate = 8000
seconds = 0.5
samples = [math.sin(2 * math.pi * 659 * i / rate) for i in range(int(rate * seconds))]
print("samples:", len(samples))
print("8-bit size:", len(samples) * 8, "bits")
rises = 0
for i in range(1, len(samples)):
if samples[i - 1] < 0 <= samples[i]:
rises = rises + 1
freq = round(rises / seconds)
print("note:", freq, "Hz")
tone(freq, 0.5)
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.