A Formula for Love

Written By: Ioana Dănilă
  • Math Software
  • 7/27/2017

Love is hard work. Love is complicated. Some even say it’s a battlefield. But as the world celebrates the recent 50th anniversary of the Beatle’s timeless song, All You Need Is Love, Ioana Dănilă (with Magnus Eklund) argue that love is not impossible. In fact, it’s easy if you know the math.

In this post, they offer up a formula for love itself. Then, they’ll show you what that Beatle’s song looks like when you turn sound into images in PTC Mathcad.

Writing Love

Oh, the Beatles! No introduction needed for this iconic 20th-century quartet. Their music, and that of their member’s solo careers, encapsulated emotions and social movements for over a half century. Listen to any one of their hits, and you’re instantly transported down memory lane.

Although there isn’t an exact formula or equation for love, we’re not letting it stop us from spelling out our Beatles fandom in Mathcad. Turns out, all you need is … a few simple trig functions.


 Graphing the word Love

See? Maybe love isn’t so complicated after all.

See the Music

To actually see the song, start with an .mp3 file. Our example uses “All You Need Is Love,” but you can use a different file. Once you have the file, convert it to a .wav format.


To create the .wav file, do a quick online search for “convert .mp3 to .wav” and choose one of the available sites. Then upload your .mp3 file and click “convert” and “download.” That’s it! 

Working with WAV Files

In the PTC Mathcad worksheet, use the READWAV and GETWAVINFO functions. Just follow these steps:

1. State the name of the sound file: file:=”All_You_Need_Is_Love.wav”

2. Use the GETWAVINFO function to read the sound file and save the info into a vector:


The vector contains the number of channels, the sample rate, the resolution (number of bits per sample), and the average number of bytes per second that an audio player device needs to process to play the audio in real time. If it returns a matrix, successive columns represent the separate channels of data.

3. Evaluate the vector variables:

Vector variables

4. Create a function for reading the file:

 READWAV function

5. Use the rows function to calculate the total number of samples, which is the total number of points contained within the length of the song.

Rows function


PTC Mathcad Prime 4.0 plots up to 2 million points within a 2D plot. Our entire song is over 10 million (rows(aynil)), so we’ll just capture the word Love” from the song. Cut it between 8.35 seconds and 9.55 seconds into the song. If you want the whole chorus, stop at the 19th second.


Now we establish sample rates and channels.

and backward:

From Music to Pictures

 It’s time to plot the signal as a graph, forward and backward.

Signal graphs

From Pictures to Music

To play back your work, just use these commands:

Writewav functions to play back your work

Or hear the channels separately:

Commands to hear separate channels

For the chorus:



For a longer period:

Longer period

Do the Math

You can do this exercise with any mp3 file and PTC Mathcad. Download PTC Mathcad Prime 4.0 now and see how the data and signals look for your favorite music. 

  • Math Software

About the Author

Ioana Dănilă

Ioana Dănilă is a PTC Mathcad Application Engineer (AE), living in Romania. She works closely with the quality and and AE teams inside PTC. When she’s not working on her PhD in Risk Modelling, she enjoys reading, travelling, flying, extreme sports, and playing the piano.

“We’re all stories, in the end.
Just make it a good one, eh?”
—Dr. Who