I’m very new to Mathematica and the Wolfram Language. I purchased a hobbyist license of Mathematica, and I’m progressing through An Elementary Introduction to the Wolfram Language. I have some background in programming, a smattering of many different languages. Along the way through the book, I come up with code ideas of my own to try new things and test what I’ve learned.
I wrote some code to generate DTMF telephone signals, and I’m wondering if there are strategies to optimize the code. Some of the syntax seems verbose:
k = "123A456B789C*0D";
f = Flatten[Table[{a, b}, {a, {697, 770, 852, 941}}, {b, {1209, 1336, 1477, 1633}}], 1]; Manipulate[
Play[Sin[f[[Position[Characters[k], x][[1]][[1]]]][[1]] 2 Pi t] +
Sin[f[[Position[Characters[k], x][[1]][[1]]]][[2]] 2 Pi t], {t, 0, 1}], {x, Characters[k], Setter}]
Hints and tips on how to make the most of Mathematica and the Wolfram Language would be most welcome. Thank you!
All those extracting parts of expressions is not easy to decipher. Perhaps you can pre-calculate the associations between the characters and the sin coefficients this way:
f = Tuples[{{697, 770, 852, 941}, {1209, 1336, 1477, 1633}}];
k = "123A456B789C*0D";
fromCharToCoeffs = AssociationThread[Characters[k] -> Most@f];
Manipulate[
Play[Sin[fromCharToCoeffs[x][[1]] 2 Pi t] +
Sin[fromCharToCoeffs[x][[2]] 2 Pi t], {t, 0, 1}], {x,
Characters[k], Setter}]
I have no idea if this will improve the speed of computation, though. Most of the time is spent on building the sound data for Play.