**Guest post by Luke Westbrook**
One of my favorite parts of Charles Dickens’ A Christmas Carol is when Ebenezer Scrooge, terrified and repentant, pleads with the third Spirit for a second chance, saying, “I will honor Christmas in my heart, and try to keep it all the year.” I don’t mean to wax philosophical, but I resonate with the idea of trying to maintain that spirit of optimism and kindness and goodwill throughout the entire year and not simply during the month of December. However, that only extends to the spirit of Christmas, not to the, shall I say, “paraphernalia” of Christmas. I enjoy the trees and the lights and the music as much as the next person, maybe even a bit more, but I maintain a pretty strict “Not Until After Thanksgiving” rule when it comes to all the Yuletide accoutrements. I only wish department stores would follow that same rule.
But Thanksgiving has come and gone, so I say, in the immortal words of Max from Where the Wild Things Are (with a minor addition by yours truly): “Let the wild [Christmas] rumpus start!” And start it has. And with all the excitement and holiday joy going around, I wanted to get PTC Mathcad in on the festive jubilation.
The question became: How? There’s the obvious approach: using PTC Mathcad to determine how fast Santa will have to travel on Christmas Eve in order to stuff stockings with goodies or *gasp* coal. Or we could calculate the tensile strength of tinsel. We could perform estimates on the average increase in household energy bills in December due to Christmas decorations. Perhaps a statistical analysis to ascertain the genetic likelihood of having a Bernard the Blue-Nosed Reindeer in the future. I could keep going, but I’m having way too much fun coming up with these ideas.
Though I may have inadvertently given us ideas for Christmas blogs for the next few years, this time around, I decided to get PTC Mathcad to play you all a song. A Christmas song, of course. “Jingle Bells,” to be specific. Although, I will say, upon reviewing the lyrics to “Jingle Bells,” I am now realizing that it’s not so much a Christmas song as it is a winter song. Oh well. Too late now.
First things first, we need some sheet music. Now, I am not particularly musically inclined, so I went with the easiest option I could find. Next, I need to know the actual frequencies of musical notes. This site takes care of that issue. Now to build my worksheet.
Searching around for typical sampling frequencies for audio recordings, I decide on
(I started with 44.1 kHz, but I get better audio quality with 96, though the WAV file is more than twice as big). After that, I define my own units for musical notes, making a quarter-note have a duration of a third of a second.
At this point, I built functions for the different musical notes. Like I said, this version of “Jingle Bells” is extremely easy, as it only uses five different notes: C, D, E, F, and G. Looking at the table of frequencies, I decided to use C5, which is one above middle C and has a frequency of 523.25 Hz.
What I have here is a function of t, which will be the duration of the note. I start with a for loop that goes in increments of 1 from zero to the duration multiplied by the sampling frequency, which results in the number of samples taken for this note. Using this loop to create a vector of the time, in seconds, at each sample taken, I then use a sine function to create a matrix of data for that note.
Once I do this for the other four notes, I then go line by line on the sheet music, stacking vectors of the notes for the appropriate durations. So for the 4th line:
Now I stack all five lines together into one matrix aptly named Jingle, and then use the WRITEWAV function to write that matrix to a WAV file. WRITEWAV takes four arguments: (1) a string of the filename that you want to save the file as (Note: if you just put the file name, PTC Mathcad will save the file in whatever the working directory is. If you don’t want this, you’ll need to actually give the file path), (2) the sample rate, which is 96 kHz, (3) the bit resolution—you’ll want to use 8—and (4) the matrix of data, Jingle. Hitting equals serves to activate the function, and voila! You now can navigate to the newly created WAV file and enjoy the Christmas carol that PTC Mathcad has made for you.
That wraps things up for me, but as I said, this is a very simple song. I’m interested in seeing what you all can make PTC Mathcad sing. Feel free to attach your worksheet and/or WAV file in a comment on my post of the Jingle Bells worksheet on the PTC Mathcad Community.
Merry Christmas, folks.
Try it out for free in PTC Mathcad Express.
Free for life version
90 second videos
Training, live demos, free worksheets