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


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_wave=2*(f*t-floor(0.5+f*t));
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_wave=(2/%pi)*asin(sin(2*%pi*f*t));
tri_file = "C:\Users\Vlad\wav\tri"+string(f)+".wav";
wavwrite(tri_wave, Fs, bits, tri_file); 

// Square wave
sq_wave=sign(sin(2*%pi*f*t));
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
n_width=100;
ks=-1+2*rand(1,n_width,"uniform");
alpha=0.96;

while ( length(ks) < n_samples )
    ks=[ks,alpha*ks($-n_width+1:$)];
end
ks=ks(1:n_samples);

We can play the generated sound in Scilab with


sound(ks)

and plot its waveform


plot(t,ks)

ks

Next: part 3

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

The URI to TrackBack this entry is: https://burubaxair.wordpress.com/2014/05/08/digital-signal-processing-in-scilab-and-xcos-part-2-playing-sound-files/trackback/

RSS feed for comments on this post.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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: