Qu

Signal processing

Signal basics

a script that runs end-to-end on the M2 interpreter.

catalog/demo_signal.qu

What it exercises

How it works

Setup

Fs = 5 kHz               # 5000 (units normalize to SI)
N  = 16
n  = 0 to N - 1          # a length-16 index vector

# a decaying cosine, built with elementwise ops
x = cos(2 * pi * n / N) * exp(-n / N)

x -= mean(x)             # detrend in place (compound assign + broadcast)

Compute

crest(v) := max(abs(v)) / rms(v)

print("N      = {N}")
print("mean x = {mean(x):.4f}")
print("rms  x = {rms(x):.4f}")
print("crest  = {crest(x):.3f}")

acc = 0
for k = 1 to N
    acc += k
end for
print("sum 1..N = {acc}")

Plot

plot(n, x)
show plot

Results

printed
N      = 16
mean x = 0.0000
rms  x = 0.4978
crest  = 1.937
sum 1..N = 136
-0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1.0 0 2 4 6 8 10 12 14
Drawn by running the program above.

The whole program

Runnable as it stands: qu run catalog/demo_signal.qu

# demo_signal.qu — a script that runs end-to-end on the M2 interpreter.

# Exercises: units, ranges, vectors, broadcasting, builtins, := functions,

# string interpolation, and a for-loop.


backend auto

#%% Setup

Fs = 5 kHz               # 5000 (units normalize to SI)

N  = 16
n  = 0 to N - 1          # a length-16 index vector


# a decaying cosine, built with elementwise ops

x = cos(2 * pi * n / N) * exp(-n / N)

x -= mean(x)             # detrend in place (compound assign + broadcast)


#%% Compute

crest(v) := max(abs(v)) / rms(v)

print("N      = {N}")
print("mean x = {mean(x):.4f}")
print("rms  x = {rms(x):.4f}")
print("crest  = {crest(x):.3f}")

acc = 0
for k = 1 to N
    acc += k
end for
print("sum 1..N = {acc}")

#%% Plot

plot(n, x)
show plot