"""Scalar encoding and decoding — Teaching Script §2.1.1. The original recording used Nengo 4.1.0 and NumPy 2.5.4. Install Nengo and NumPy, then run: python scalar-example.py Writes scalar-data.npz to the current directory. No added noise; all spikes and readouts come from the Nengo simulation. """ import nengo import numpy as np def level(t): """Normalized sound-level envelope, followed by a constant level.""" q = min(t, 1.0) return .52 + .12 * np.sin(2 * np.pi * q) + .035 * np.sin(4 * np.pi * q) with nengo.Network(seed=105, label="Scalar representation teaching example") as model: stimulus = nengo.Node(level, label="Normalized sound level") neurons = nengo.Ensemble( 50, dimensions=1, radius=1.0, neuron_type=nengo.LIF(), max_rates=nengo.dists.Uniform(80, 160), label="50 spiking neurons") nengo.Connection(stimulus, neurons, synapse=None) readout = nengo.Node(size_in=1, label="Decoded, filtered observation") connection = nengo.Connection(neurons, readout, synapse=0.010) input_probe = nengo.Probe(stimulus) spike_probe = nengo.Probe(neurons.neurons) readout_probe = nengo.Probe(readout) with nengo.Simulator(model, dt=0.001, progress_bar=False) as sim: sim.run(1.6) np.savez_compressed( "scalar-data.npz", time=sim.trange(), input=sim.data[input_probe][:, 0], decoded=sim.data[readout_probe][:, 0], spikes=sim.data[spike_probe], decoder_weights=sim.data[connection].weights.copy()) print("Saved 1,600 samples and all 50 spike trains to scalar-data.npz.")