The Application of PC Soundcard as A Data Acquisition Device Controlled by MATLAB Data Acquisition Toolbox

posted in: Technical | 0

The report provides explanation on the design and application of a PC soundcard used as a data acquisition device controlled by Matlab Data Acquisition Toolbox. The idea was based on the needs to apply an economic and reliable data acquisition device to digitize the analogue audible sound wave which can be stored for further analysis. The report covers explanations on the area of Matlab programming in assigning the soundcard as a data acquisition hardware, processing the acquired data in Matlab workspace, plotting the acquired data and technique to store the data acquisition results into hard drive for advanced application and analysis outside Matlab programming environment.
Keywords: Data Acquisition, Matlab, PC Soundcard.

Background

In this report a discussion regarding the application of a build-in regular PC soundcard as data acquisition device is explained. The main idea to apply the soundcard as a data acquisition device was based on the fact that getting a special data acquisition board or device used to digitize analogue information of sound wave is expensive. Therefore, the option to use the regular PC soundcard is a reasonable option to capture the magnitude of analogue sound wave by using a microphone connected to soundcard and then convert the captured information into digital form (digitalization). Another reason to use the soundcard was that it becomes an integrated component in every PC used nowadays. Hence, there will be no problem in finding a soundcard attached to a PC that will be used to digitize audible sound wave. Data Acquisition Toolbox from Matlab was used as the software to control the application of a PC soundcard in the digitalization process of audible sound wave received via a microphone. In this design, Matlab was the software used to control and regulate the digitalization process using a soundcard which is applied as the hardware.

Components of Matlab Data Acquisition Toolbox

The Data Acquisition Toolbox consists of three distinct components: M-file functions, the data acquisition engine, and hardware driver adaptors. These components and relationships are shown in Figure 1.

Figure 1. Matlab Data Acquisition Toolbox Components

Matlab DAQ Engine blocks

To perform any data acquisition task, user must first call one or more M-file functions from the MATLAB environment. The M-files that user can call depend on user’s specific data acquisition task. These functions allow user to:

  • Create device objects which provide a gateway to all of the hardware’s functionality and allow you to control the behavior of the acquisition.

  • Control your acquisition behavior by passing information to, or receiving information from the hardware.

  • Evaluate the acquisition status and resources by requesting information from the hardware.

The information passed to, or requested from, the hardware is in the form of property values or data. Property values reflect the full functionality of both the Data acquisition Toolbox and the hardware.

Data Acquisition Engine

The data acquisition engine (or just engine) is a MEX-file DLL that:

  • Stores the device objects and associated property values that control data acquisition application

  • Controls the synchronization of events

  • Controls the storage of acquired data to disk or memory

The Hardware Driver Adaptor

The hardware driver adaptor (or just adaptor) is the interface between the data acquisition engine and the hardware driver. The adaptor allows you to:

  • Pass information from the engine to the hardware driver.

  • Pass information from the hardware driver to the engine.

Supported Toolbox Adaptors

The Data Acquisition Toolbox provides adaptors for sound cards and National Instruments E-series hardware. The supported devices and adaptor name used by the toolbox are listed  below.

Device: PC sound card —> Adaptor name: winsound
Device: National Instruments —> Adaptor name: nidaq

As it shown above, Matlab Data Acquisition Toolbox support the application of a PC soundcard as a toolbox devices used in acquiring data from the outside world. The adaptor name of the device is winsound. Based on the default list of supported toolbox devices, hence any PC soundcard operated in a machine with Windows® as the operating system can be accessed, applied and controlled by Matlab Data Acquisition Toolbox. The schematic arrangement of data acquisition system using a PC soundcard which controlled by Matlab Data Acquisition Toolbox is shown in Figure 2.

Diagram block of Data Acquisition System

The system takes the audible sound wave information from data source by using a sensor. A microphone is used as sensor that connected to the soundcard in the PC. Matlab Data Acquisition Toolbox software was used to control and process the analogue to digital conversion (A/D) of sound wave received by the microphone. The converted data the processed in Matlab workspace and plotted using Matlab figure function. The complete Matlab Code to operate of the data acquisition system as shown in Figure 2 is displayed in parts are shown below.

% ====================== Part 1 ============================
% Matlab Program for Data Acquisition Using PC's Sound Card
% File name: soundaq2k4_hx.m
% Begin Matlab codes

kartu_suara = analoginput('winsound');
addchannel(kartu_suara, 1:2);
set(kartu_suara,'SampleRate', 8000);
set(kartu_suara,'SamplesPerTrigger', 8000);
start(kartu_suara);
data_suara = getdata(kartu_suara);
delete(kartu_suara);

Part 1 of the complete code consists of Matlab commands used to apply the two analogue inputs (left and right channels) of the soundcard. The two analogue inputs are connected in parallel to a microphone which used to capture the audible sound wave generated by an audio source (i.e., tunning fork or sound of music from stereo set). This part is the main part of data acquisition engine in which it contains information of how many analogue channel of the sound card is used and the sampling rate applied to capture and digitised the analogue audible sound wave. In this application, the sampling rate used is 8000 samples per second. This rate is the minimum sample rate that can be assigned to a PC soundcard. Part 1 of the complete code contains commands to start and to stop data acquisition process using the soundcard. This part also contains command to get captured sound wave data from soundcard and save the data in the form of 8000 x 2 size matrix. At the end of Part 1, there is a command issued to delete unused data acquisition object from Matlab workspace. The deletion of unused data acquisition object is carried out to free the memory available for the next session of data acquisition process.

kalender_skrg = now;
bulan_skrg = datestr(kalender_skrg, 3);
hari_skrg = datestr(kalender_skrg, 7);
jam_skrg = datestr(kalender_skrg, 13);

% ====================== Part 3 ============================
waktu_skrg = jam_skrg([1 2 4 5 7 8]);
angka_jam_skrg = str2num(jam_skrg([1 2]));

% ====================== Part 4 ============================
if angka_jam_skrg == angka_jam_sblm
disp(‘Belum Lewat Satu Jam’);
[s,w]=dos(‘dir d:\temp\daq_data\’, ‘-echo’);
elseif angka_jam_skrg > angka_jam_sblm
[s,w]=dos(‘del d:\temp\daq_data\*.txt’);
disp(‘Semua file txt dari Sejam sebelumnya dihapus’);
elseif angka_jam_skrg < angka_jam_sblm
disp(‘Peralihan dari jam 23:59:59 PM ke jam 00:00:00 AM’);
save ‘d:\temp\raw_data_test.txt’ data_suara -ASCII;
else
disp(‘Kondisi Awal – Inisialisasi Program’);
end

The main function of Part 2, 3, and 4 of the complete Matlab code are to interrogate the operating system to obtain data of current calendar date and time. The current date information is consists of month and day while the time information consists of hour, minute and second (i.e., hh:mm:ss). The information of month, day, hour, minute and second are specially provided which to be used in the composition of file name which used to save the digitised numeric data in the form of a text file into the hard drive of the PC.

% ====================== Part 5 ============================
nama_folder = 'd:\temp\daq_data\';file_ekstension = '.txt';
nama_file = [nama_folder,'rw_dt_',num2str(hari_skrg),...'_', num2str(bulan_skrg),'_',waktu_skrg, file_ekstension];
eval(['save ', nama_file ,' data_suara -ASCII']);

Part 5 of the complete Matlab code consists of commands to specify the location in the hard drive in which the results of digitised data are to be stored or saved. The type of the computer file used to save the results of data acquisition session is the text (.txt) file. The option to save data acquisition file in the text format is based on the fact that text format file can opened easily using other application software used to manipulate documents and text in Windows® operating system. The convention used to name the resulted data acquisition file is based on the rule: string rw_dt (stands for raw data) joined with string of current date, joined with string of current month in three letters format, joined with current time in hh:mm:ss format, and ended with file extension which is .txt. Underscore marks are inserted between month, day and time in the composition of file name to increase the readability of the file name constructed. As an example, a saved file which is named as rw_dt_11_Sep_180036.txt is a data acquisition which generated at September 11 at 18 hour 00 minute 36 seconds. This naming convention was made hence different data resulted from different data acquisition process can be stored with specific file name which then is easy to find in the hard drive for further analysis.

% ====================== Part 6 ============================
data_suara1 = data_suara(:,1);
data_suara2 = data_suara(:,2);
max_1 = max(data_suara1);max_2 = max(data_suara2);min_1 = min(data_suara1);
min_2 = min(data_suara2);

Part 6 shown above contains Matlab commands used divide 8000 x 2 size matrix into two new data matrices form which represent data from left and right channel of the soundcard separately. Both of the new data matrices are in the form of 8000 x 1 (row, column) size matrix which hold information of digitized audible sound wave captured by microphone which connected to left and right channel of the soundcard. The number 8000 represents amount of data converted from analogue to digital form within 1 second duration. The term digital refers to numerical data in the form of 8000 x 1 size matrix which reside in Matlab workspace or in PC memory (RAM). The separation of sound wave data from left and right channels of the soundcard is made to accommodate functions to find maximum and minimum values from the data which carried out in Part 7 and 8.

% ====================== Part 7 ============================
if max_1 > max_2
sinyal_maksimum = max_1;
cari_maksimum = find(max_1 == data_suara1);
elseif max_1 < max_2
sinyal_maksimum = max_2;
cari_maksimum = find(max_2 == data_suara2);
elseif max_1 == max_2
sinyal_maksimum = max_1;
cari_maksimum = find(max_1 == data_suara1);
disp('Nilai Max 1 = Max 2');
else
end

% ====================== Part 8 ============================
if min_1 > min_2
sinyal_minimum = min_2;
cari_minimum = find(min_2 == data_suara2);
elseif min_1 < min_2
sinyal_minimum = min_1;
cari_minimum = find(min_1 == data_suara1);
elseif min_1 == min_2
sinyal_minimum = min_1;
cari_minimum = find(min_1 == data_suara1);
disp('Min 1 = Min 2');
else
end

Part 7 and 8 of the completed code contains Matlab command used to find the maximum and minimum values from the two new matrices formed in Part 6 which used to hold information of sound wave recorded from left and right channel of the soundcard. The maximum and minimum values found will be used in Part 9 of the completed code which functions to plot the overall result of data acquisition process.

% ====================== Part 9 ============================
plot(data_suara);
hold on;
plot(cari_maksimum, sinyal_maksimum, 'ro');
hold on;
plot(cari_minimum, sinyal_minimum, 'bo');
hold off;
xlabel('\bfBanyaknya Sampel Yg Diambil ');
ylabel('\bfKuat Sinyal Dlm Volt');
judul = ['\bfData Suara Utk ',bulan_skrg,' ' ...
hari_skrg,',',' 2004 - ','Jam ',jam_skrg];
title(judul);

% ====================== Part 10 ===========================
maks = num2str(sinyal_maksimum);
minim = num2str(sinyal_minimum);

% ====================== Part 11 ===========================
atas = [‘\bfmax = ‘];
maksimum = [atas maks];
text(3000,sinyal_maksimum,maksimum);

% ====================== Part 12 ===========================
bawah = [‘\bfmin = ‘];
minimum = [bawah minim];
text(3000,sinyal_minimum,minimum);

Part 9 functions to perform plotting or visualization of the data acquisition result which currently reside in Matlab workspace or PC memory (RAM). The plotting function is enhanced by additional features provide in Part 10 and 11. These additional functions are ability to show information regarding date and time of which the data was recorded in the plot result. The additional feature also provides function to draw points and strings of which the minimum and maximum values are occurred in the plotted data.

% ====================== Part 13 ===========================
angka_jam_sblm = str2num(jam_skrg([1 2]));

The last part of the completed Matlab code (Part 13) contains a command to extract information of the hour in two digits format which to be used in Part 4. The hour information provided in Part 13 is needed to evaluate the true or false conditions of if-then options embedded in Part 4. The if-then options in Part 4 are used to erase all text files resulted from data acquisition of the previous hour if one hour period of data acquisition has passed.

Automating the Data Acquisition Event

The automation of the data acquisition event provides a feature to the system in which data acquisition process will be automatically carried out within specified interval in minutes. For example, the data acquisition can be set to occur every 300 seconds or every 5 minutes. The automatic execution of data acquisition process is provided by a Matlab m-files shown below.

% Matlab Program for Data Acquisition Using PC’s Sound Card
% File name: mulai_daqhx.m
% This file functions to automatically call % the execution of file:
% soundaq2k4_hx.m every 600 seconds
%

waktu_hx = timer('Period', 300, 'Name','DAQ_Timer_hx', ... 'TimerFcn','soundaq2k4_hx','ExecutionMode',fixedRate');
angka_jam_sblm = [];
start(waktu_hx);

The above Matlab codes contains commands to set up a timer for execution period in 300 seconds. Once the start command has been issued the timer function will execute the callback instruction to run file soundaq2k4_hx.m which operates the PC soundcard data acquisition system automatically every 300 seconds or 5 minutes time interval.

The Graphical Example of Data Acquisition Result

There are two type results for each data acquisition event which occurs in every predetermined time interval. The first type data acquisition result is the graphical result which shows the visualisation of acquired sound wave pattern. The second result is a text file named based on the date and time of the data acquisition occurs which contains a data matrix of 8000 x 2 numerical information of the acquired shown in graphically form. The text file is stored or saved in the hard drive which can be used to export the numerical data to other application such Microsoft Excel® for another purposes based on the needs. The example of graphical data acquisition is depicted in Figure 3.

Graphical Example of Data Acquisition Result

Figure 3. Graphical Example of Data Acquisition Result for the sound ‘je’

At top of the plot, there is detailed information regarding the date and time at when the acquired data was taken. The x–axis of the plot represents numbers of sampled data taken and the y-axis represents signal strength in Volt for the acquired sound wave pattern. The plotted data acquisition result is also added with information which shows the maximum and minimum values of the acquired sound wave pattern. The second type or form of the acquired data was saved by the data acquisition system into the specific directory in the hard drive. The file name was generated according to the information of date and time at which the data acquisition event occurs. Figure 4 shows the similar data acquisition result as in Figure 3 by which it was taken from resulted text file named rw_dt_12_Sept_201813.txt. The text file was opened and formatted using Microsoft Excel® and then plotted using Excel® plotting function, as shown in Figure 4.

Data Acquisition Results Exported

Figure 4. Data Acquisition Results Exported and Plotted Using MS Excel®

Concluding Remarks

  1. The minimum sampling rate in stereo mode (two channel) for a soundcard controlled by Matlab Data Acquisition Toolbox is 8000 Hz (8 KHz).

  2. The maximum sampling rate in stereo mode (two channel) for a soundcard controlled by Matlab Data Acquisition Toolbox is 44100 Hz (44,1 KHz).

  3. The data acquisition system designed and explained in this report was build using IBM Aptiva 60A with Pentium™ III 450 MHz Processor, RAM 64 MB, Running Windows® 98 and Matlab Version 6.5.1

  4. The soundcard used was an ESS AudioDrive Record (8400) from ESS Technology Inc.

Bibliography

  1. Mathworks Inc., 1999, Getting Started with Matlab Version 5.0, The Mathworks Incorporated, Natick, MA – USA.
  2. Mathworks Inc., 1999, Matlab Data Acquisition Toolbox Version 1.0: User’s Guide, The Mathworks Incorporated, Natick, MA – USA.
  3. Mathworks Inc., 1999, Using Matlab Version 5.0, The Mathworks Incorporated, Natick, MA – USA.
  4. Mathworks Inc., 1999, Using Matlab Graphics Version 5.3, The Mathworks Incorporated, Natick, MA – USA.

Written by: Jonny Latuny – e-mail: latunyj@yahoo.com.au
Mechanical Engineering Department
Pattimura University, Ambon – Maluku 97000