Digital signal processing in Scilab and Xcos. Part 1: Reading sound files

In this part, we will load a *.wav sound file into a Scilab’s workspace, and then plot it in Xcos.

Let’s read a *.wav file into a Scilab’s workspace.

I do it in the Scilab’s editor in order to better organize my code and to save it after I exit Scilab.

Type into the workspace


editor

In the editor, type the command for reading a *.wav file:


[y,Fs,bits] = wavread(...
    "C:\Users\Vlad\R.A.L.E\Wheezes\wheezing_c.wav"...
) // reading the sound file

The file that I use for this example, wheezing_c.wav, is a recording of an auscultation over trachea and right lower lung of an 11-year old girl with acute asthma (taken from the R.A.L.E. repository).

wavread returns the sampled data in y, the sampling rate Fs in Hertz (number of samples per second) and bits — the number of bits per sample used to encode the data.

Checking our data,


disp(size(y),"size = ")
disp(Fs,"sampling rate = ")
disp(bits,"bits per sample = ")
disp("Range = ["+string(min(y))+", "+string(max(y))+"]")

we get


 size =    
 
    2.    110243.  
 
 sampling rate =    
 
    11025.  
 
 bits per sample =    
 
    16.  
 
 Range = [-0.4912720, 0.4161072]

meaning that the sound file consists of two channels. Each channel contains 110243 16-bit samples. The sampling rate is 11025 times per second. The numeric values of the data vary between -0.5 and 0.5

To calculate the time duration of our signal, we need to divide the total number of samples by the sampling rate:


n_samples = length(y(1,:));
t_total = n_samples / Fs;

For this file, t_total is 10 seconds.

Let’s now create an array of time points at which the samples were taken:


t = linspace(0, t_total, n_samples);

Always remember that to plot the data, we need to make sure that the number of sampling time points should be equal to the number of data in our signal.

To open the data in Xcos, we have to put each channel into a separate structure:


channel_1.time = t';
channel_1.values = y(1,:)';

channel_2.time = t';
channel_2.values = y(2,:)';

Notice that each field in these structures is a column vector.

Save your code in the editor in a file (I called mine wav_example.sce) and execute it so that all calculated variables were in the workspace.


exec('C:\Users\Vlad\wav_example.sce', -1)

Then open Xcos by typing


xcos

into the workspace.

Build the following Xcos diagram

wav_example_1

The block parameters are as follows.

For the first channel:
wav_example_2

For the second channel:
wav_example_3

For the clock:
wav_example_4

The clock period is the reciprocal of the sampling frequency, 1/Fs.

The parameters of the figure that I changed:

Ymin vector -0.5 -0.5
Ymax vector 0.5 0.5

(because our data is between -0.5 and 0.5)

Refresh period 10 10

I also chose the colour of both curves to be green (colour value = 3).

Then go to the Xcos’ toolbar and choose

Simulation –> Setup

Set the value of the final integration time to 10.

Run the simulation.

You should get the picture of the signals in both channels in the time domain.

wav_example_5

Now, as you’ve got your signal values in Xcos, you can feed them to all kinds of filters and do other interesting things.

Remember that after starting a new session in Scilab, you have to first execute the file wav_example.sce to load all your variables into the Scilab’s worksheet before starting the simulations of this diagram in Xcos.

Read a few more tips on this topic here: Xcos, Scilab Workspace and Data-file Integration (by http://www.equalis.com).

Next: part 2, part 3

Advertisements
Published in: on 19/04/2014 at 20:27  Comments (1)  
Tags: ,

The URI to TrackBack this entry is: https://burubaxair.wordpress.com/2014/04/19/digital-signal-processing-in-scilab-and-xcos-part-1-reading-sound-files/trackback/

RSS feed for comments on this post.

One CommentLeave a comment


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: