Mathematica: Edit and save WAV files
With Wolfram Mathematica you can import, analyze, edit and export WAV files. We'll tell you how.
Import WAV files into Mathematica
With the simple command "audio = Import [path, command]", you can import data from WAV files into Mathematica. The path and command must be quoted. Help for easy path entry is given in the picture gallery. You can load various elements of the WAV file with the following commands:
- "Elements" shows you which information of the WAV file you can import into Mathematica
- "AudioChannels" informs you about the number of channels embedded in the WAV file.
- "SampleRate" outputs the sample rate
- "AudioEncoding" indicates how the samples are encoded. As a rule, the value lies between "Integer16" and "Integer32", ie integers with 16 to 32 bits. Encodings from 4 to 64 bits are possible.
- "Data" is a matrix that contains the RAW data of a channel in each line. Warning: Since this matrix contains all of the audio material, it can sometimes be very large. Files up to 4 GB can be loaded. Here you should definitely use ";" suppress the output of the data. Since you gave the matrix the name "audio", you can continue to work with it.
- Using "Sound" Mathematica loads the file including waveform and spectrum display and a small player to play the sound.
- With "Rules" all information is imported into a matrix. Six lines contain "AudioChannels", "AudioEncoding", "Data", "SampledSoundList", "SampleRate" and "Sound" in this order. Caution: This matrix is significantly larger than the audio file itself, so you should definitely output the data with a ";" suppress.
Analyze and edit WAV files in Mathematica
The RAW data imported using "Data" can be used and edited in Mathematica like any other table. The following commands are particularly interesting for audio material:
- You can now display any value in the "audio" matrix and change it as you wish.
- "Dimensions [audio]" gives you the number of rows and columns, ie the number of samples per channel.
- Using "area = Take [audio [[X, All]], {start value, end value}];" you can remove part of channel X.
- "Spectrum = Fourier [area];" does a Fourier analysis of this part.
- You can also change spectral components at will.
- "Re [spectrum]", "Im [spectrum]", "Abs [spectrum]" and "Arg [spectrum]" output the real part, imaginary part, amplitude and phase of the spectral components.
Export WAV files from Mathematica
To make the manipulated audio data a WAV file again, simply enter "Export [path, command]". Use the following commands to set the WAV file correctly:
- Before exporting, you should use "audio / = Max [Abs [audio];]" on | 1 | normalize. Values above 1 and below -1 are overridden.
- "" SampleRate "-> 44100" exports the audio material at 44100 samples per second.
- "" AudioEncoding "->" Integer16 "" encodes the audio file with 16 bits per sample.
- With "" AudioChannels "-> 2" you can ensure that only the first 2 channels are exported, even if the audio material contains more channels.
You can find tips on Wolfram Alpha here. How to work with the Audacity program. we explain here.
Latest videos
Entering paths in Mathematica is a bit tedious: paths are enclosed in quotation marks and folders are not separated by a slash symbol "/" but by two backslashes "\\". Simply copying the path from Explorer does not work here.
The easiest way is to use "Insert" -> "Filepath
" choose... "Filepath
"choose ...">
"Filepath
"choose ..." />
"choose ..." />
and navigate here in the explorer window to the desired file. Mathematica then notes the file path in the correct spelling.