Digital signal processing in Scilab and Xcos. Part 2: Playing sound files

In my previous post on DSP in Scilab, I gave an example on how to open a wav file and make it available for further processing.

Today we’ll discuss the opposite task: how to generate signals and store them into wav files.

Open the Scilab’s editor


Choose the sampling rate, the number of bits per sample, and the time duration for your signals

Fs = 11025; // samples per second
bits = 16; // bits per sample
t_total = 10; // seconds

The total number of samples in the signal:

n_samples = Fs * t_total;

Create an array of time points at which the samples will be synthesized:

t = linspace(0, t_total, n_samples);

The following code will generate a 440 Hz sine wave, and save it into a wav file sin440.wav

f=440; // sound frequency

// Sine wave
sin_wave = sin(2*%pi*f*t);
sin_file = "C:\Users\Vlad\wav\sin"+string(f)+".wav";
wavwrite(sin_wave, Fs, bits, sin_file);

Let’s now generate a sawtooth wave of the same frequency and output it into another wav file:

// Sawtooth wave
saw_file = "C:\Users\Vlad\wav\saw"+string(f)+".wav";
wavwrite(saw_wave, Fs, bits, saw_file);

Similarly, for a triangle and a square waves:

// Triangle wave
tri_file = "C:\Users\Vlad\wav\tri"+string(f)+".wav";
wavwrite(tri_wave, Fs, bits, tri_file); 

// Square wave
sq_file = "C:\Users\Vlad\wav\sq"+string(f)+".wav";
wavwrite(sq_wave, Fs, bits, sq_file); 

Let’s simulate a guitar sound using the Karplus–Strong string synthesis method.

// Karplus-Strong

while ( length(ks) < n_samples )

We can play the generated sound in Scilab with


and plot its waveform



Next: part 3

Published in: on 08/05/2014 at 19:36  Leave a Comment  

The URI to TrackBack this entry is:

RSS feed for comments on this post.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: