Site menu Musical Morse

Musical Morse

In the last months, there has been a trickle of tweets saying nice things about the sound of my Web Morse player. Initially written as an example of usage of the then-incipient Web Audio API, I have paid more attention to it in the wake of a personal interest (ham radio license).

Besides the Javascript code being a good example of real-time audio generation and WAV generation for download, I never felt someone would notice the audio quality. But for sure I put some effort on this aspect, it did not come for free.

The audios below were generated by that page. One has the "secret sauce", the other does not. Can you tell them apart?

(link to audio)

(link to audio)

There are no secrets in the secret sauce, it is simply fade-in an fade-out on each beep. The ramp is just 2ms long. It is enough to avoid the clicks that you probably heard in the unfiltered audio.

In the figures below, we have the waveforms with and without fade-in. Following the Morse tradition, the base tone is sinusoidal.

Figure 1: Sinusoidal wave with abrupt start

Figure 2: Sinusoidal wave with fade-in

As I said, the filtering method applied to Morse audio is trivial. In my code, it was implemented as a simple linear ramp.

The risk of using fade ramps on each beep is to "blur" the Morse signal, rendering it impossible to understand, particularly in higher speeds. Dots and dashes still need to have enough "attack" or "impacto" to be heard distinctly. To mitigate this risk, I use steeper ramps for higher speeds, but I am not sure the results are good enough for speeds above 50WPM.

There is potential on different ramp functions. The choice of a "better" ramp is not purely objective: the "musicality" of the Morse, balanced in smoothness and clarity, is the final criteria. Instead of a ramp, we could employ a filter.

Anyway, a spectral analysis shows that the humble linear ramp already does a nice job. The next figure shows the spectrum of a short beep with abrupt start and stop. Audio is 800Hz, so we can say that only the spectrum around 800Hz is desired, the rest is noise:

Figure 3: Sinusoidal beep spectrum, abrupt start/stop

The next spectrum is the same beep, smoothed by a short fade-in and equally short fade-out:

Figure 4: Sinusoidal beep spectrum with fade-in and fade-out

The smoothed spectrum has a higher peak around 800Hz, with less noise scattered on other frequencies, particularly less at high frequencies where the most annoying noises lie. The abrupt beep scatters noise up to 7000Hz while the smoothed one goes no higher than 3500Hz.


After I finished the first version of this text, I couldn't let go the idea of trying different ramps. I don't remember where I first read it, but a popular ramp for Morse is the "squared sine". In hearing tests, it was difficult to say whether it was so much better than linear, but certainly it was not worse. I didn't make a double-blind test.

The spectrum analyzer liked the change. The figure below is the spectrum of a Morse audio with 35WPM and Fansworth compression of 60WPM:

Figure 5: Morse audio spectrum, 35WPM/60WPM, 2ms linear ramp

Now, the spectrum with squared-sine ramp of the same length (2ms):

Figure 6: Morse audio spectrum, 35WPM/60WPM, 2ms sine-squared ramp

As it can be seen, the second spectrum is less "scattered".

After this test, I looked for more information, to be sure that I was going the right way. I've found an excellent article, and the CWStudio library which generates a beautiful Morse audio and can even simulate typical radio distortions (harmonics, white noise, signal fading, etc.).

Interestingly, CWStudio uses a simple senoid ramp (not squared), with good results. Perhaps I should add an option to the Morse page, so people could try different ramps.


Darwin said that "nature does not jump". In the context of this page, I would say that nature does not like to jump. This is the essence of the improvement obtained by fading in and out.

In nature, the only kind of wave that can travel is the sinusoidal wave. It does not matter whether it is a sound wave, a radio wave, etc. Due to this, the spectral analysis (like the figures shown above) is only concerned about sinusoidal waves. We can conceive square or triangular waves, but they can't be played by a loudspeaker, not heard by our ears.

The square wave is the most "anti-natural" of all, since all transitions are instant. Nothing in the real world works nearly like a square wave. If you try to move an object from A to B instantly, it is destroyed. The same happens if you try to change the object's speed, or even the acceleration, too fast.

But why the sinusoidal wave is the "chosen one"? It is due to the mathematical properties. Putting it simply, the sinusoidal wave is "smooth" in all aspects. It has no kinks, no abrupt transition, and of course it is periodic (a basic requisite for waves).

But we know that a square wave sounds different from a triangular one, that sounds different from a sinusoid. Somehow the non-natural waves can travel. The solution found by nature is the use of harmonics, sinusiodal waves of many different frequencies, whose sum re-creates the original wave. A square wave cannot travel as it is, but it is fragmented in many sinusoids by the loudspeaker, which can travel as (separate) soundwaves. The ear picks up the sinusoids, and the brain labels the whole mix as "square wave timbre".

All this process of fragmentation and reassembly can be carried out matematically by using the Fourier Transform. This is the tool used by spectrum analyzers, and it supplies the theoretical basis for a common natural phenomenon.

The sinusoidal function is continuous, it has no beginning and no end. So, a "beep" cannot be a pure sinusoid, and already needs harmonics to be transmitted. Which harmonics does it need? It depends on how abruptly the beep starts (or stops). If the start is 100%, the kinked profile is much like a square wave that scatters harmonics all over the place. On the other hand, a gradual start puts most harmonics around the fundamental frequency.

The opposite is also true: a heavily smoothed/filtered Morse signal tends to sound "blurry" because the excessive removal of harmonics leaves more and more of the fundamental, continuous sinusoid. And a continuous signal does not carry information.

The article I mentioned before evaluates different filters, in order to find one that does not "smear" the Morse signal i.e. by adding fade-in/fade-out ramps without scattering or adding oscilations. The article finds that the Gaussian filter is the best, and the resulting ramp looks pretty much like the sine squared.

It is possible to determine matematically what happens to a Morse signal when it is smoothed out, without using empirical methods like I did. The signal that really carries information is low-frequency: it is the square wave generated by the telegraphist. If there are ramps, the signal wave is shaped after them, becoming a triangular/trapezoidal signal for linear ramps, or sinusoids for sinusoidal ramps.

This signal is modulated by the continuous Morse tone, exactly like in AM modulation. We know that AM modulation creates two "copies" of the original signal around the carrier, as can be seen in all spectrum figures along this text. The wider the bandwidth of the original signal, the more scattered will be the modulated result. The most extremem case is the square wave (e.g. unsmoothed Morse, without ramps) whose bandwidth is the largest.