I have been learning BBC Basic and set out to do a project to test some DSP algorithms and thought I would share the progress. I have developed a program that generates a sawtooth wave wave made up of harmonics all the way up to the Nyquist frequency, it then plots the output sawtooth as red and all the harmonic sines that make up the sawtooth as white.
I intended to add generation of other waveforms such as square and triangle, and allow the parameters such as the fundamental frequency and sample rate to be set from the UI. I also eventually plan to get the waveforms playable as audio at some point.
The array lib has been pretty useful for this project, I recommend using it if you are doing projects where you are using arrays and doing operations on them like multiplication.
I've now got it to animate the composition of a sawtooth via adding the harmonics one by one so you can see how the output wave is composed by adding successive harmonics.
DDRM wrote: ↑Tue 31 Aug 2021, 09:56
Nice! Quite impressive how fast it gets quite close to a sawtooth.
D
Thanks! @DDRM
I was quite surprised at how fast it was generating that sawtooth up to Nyquist just adding SIN()'s. I could go further but I'm basing it off CD quality 44100hz sample rate.
I intend to look at getting it to play back via audio at some point.
Has been a really fun little project to do to get into BBC Basic.
Repton wrote: ↑Sun 26 Jan 2025, 13:11
Hi Do you have the source code?
In case you don't get a response from the OP, it's easy enough to work out from first principles using the hints he gives (e.g. to use the arraylib library):
REM Synthesise a sawtooth waveform from its Fourier series
REM By Richard Russell, inspired by a program by MrHiggins
MODE 8
INSTALL @lib$ + "arraylib"
DIM a(320 * 640), s(639), t(639)
REM Construct harmonics up to Nyquist limit:
P% = 0
FOR H% = 0 TO 319
FOR I% = -320 TO 319
a(P%) = SIN(2 * PI * H% * I% / 640)
P% += 1
NEXT
NEXT H%
REM Build sawtooth from Fourier series:
*REFRESH OFF
FOR H% = 1 TO 319
CLS
PROC_arrayslice(a(), b(), H% * 640, 640)
a = (-1) ^ H%
t() = b() * a / H%
s() += t()
FOR I% = 0 TO 639
GCOL 15
PLOT I% * 2, t(I%) * 300 + 512
GCOL 9
PLOT I% * 2, s(I%) * 300 + 512
NEXT
*REFRESH
NEXT H%
*REFRESH ON
(animated GIF)
sawtooth.gif
You do not have the required permissions to view the files attached to this post.
Richard Russell wrote: ↑Sun 26 Jan 2025, 15:19
it's easy enough to work out from first principles
Run it in your own browser here to get a feel for how fast it is in real-time (I deliberately slowed down the animated GIF to about half actual speed and had to truncate it to reduce the file size).