LOADING AND SAVING DATA

Một phần của tài liệu Digital signal processing using MATLAB for students and researchers john w leis (Trang 39 - 43)

Note that the axis limits could also be set using axis ([0 2 * pi −3 3]); . The graph now looks as shown in Figure 2.2 . There are a number of other plotting functions that are useful, including mesh() and surf() for three - dimensional plots, and contour() and subplot() for multiple small plots in a group.

2.10 LOADING AND SAVING DATA

It is often necessary to load some data from a fi le, to save calculated data, or just to save the current variables from an interactive session for later resumption. The simplest case is to type save myfi le , which will save the current variables to a fi le myfi le.mat . Typing load myfi le will retrieve them. However, what about data fi les which may be supplied via some other means?

A distinction must be made between binary and text (or “ ASCII ” ) formatted fi les. Text fi les contain plain, readable text that can be viewed with any text editor.

A simple text fi le, such as mydata.dat containing plain text on each line, might be as follows:

FIGURE 2.2 MATLAB plotting example (continued).

0 1.5708 3.1416 4.7124 6.2832

−3

−2

−1 0 1 2 3

Angle

Amplitude

Some example data waveforms

Wave 1 Wave 2

set ( gca , ‘ xlim ’ , [0 2 * pi ]);

set ( gca , ‘ xtick ’ , [0 0.25 0.5 0.75 1] * 2 * pi );

set ( gca , ‘ ylim ’ , [ −3 3]);

line one line two

This data fi le may be read using

We would like the extent of the x - axis to encompass the entire plot and not to have the legend box partially obscure the plot. The MATLAB function gca (get current axes) is useful for that:

c02.indd 31

c02.indd 31 4/13/2011 5:20:50 PM4/13/2011 5:20:50 PM

32 CHAPTER 2 MATLAB FOR SIGNAL PROCESSING

Note how the value − 1 is returned if we attempt to read past the end of the fi le. To read a formatted text data fi le, we must open the fi le, read the data, and close the fi le. Suppose now we have a data fi le myfi le.dat containing numeric data:

FileId = fopen ( ‘ mydata.dat ’ , ‘ r ’ );

s = fgetl (FileId) s =

line one s = fgetl (FileId) s =

line two s = fgetl (FileId) s =

−1

fclose (FileId);

23.4 45.6 76.8

We can use the fscanf () function, in conjunction with the formatting speci- fi er %f , to interpret the text as a fl oating - point number:

FileId = fopen ( ‘ mydata.dat ’ , ‘ r ’ );

DataPoints = fscanf (FileId, ‘ %f ’ );

fclose (FileId);

DataPoints DataPoints =

23.4000 45.6000 76.8000

It ’ s always a good idea to check the value returned from fopen () to ensure that the fi le exists. In an m - fi le, we could use:

FileName = ‘ mydata.dat ’ ; FileId = fopen (FileName, ‘ r ’ );

if ( FileId < 0 )

fprintf (1, ‘ %s does not exist\n ’ , FileName);

error ( ‘ open fi le ’ );

end

c02.indd 32

c02.indd 32 4/13/2011 5:20:51 PM4/13/2011 5:20:51 PM

2.10 LOADING AND SAVING DATA 33 To write (output) to text fi les, we can use the fprintf() function and open the fi le in write ( w ) mode:

M = rand (4, 2) M =

0.7012 0.0475 0.9103 0.7361 0.7622 0.3282 0.2625 0.6326

FileId = fopen ( ‘ mydata.dat ’ , ‘ w ’ );

fprintf (FileId, ‘ %.2f\n ’ , M);

fclose (FileId);

This gives an output fi le, mydata.dat , containing only one column (one value per line) as follows:

0.70 0.91 0.76 0.26 0.05 0.74 0.33 0.63

It appears that MATLAB has not written the data in the “ expected ” format.

The reason is that MATLAB takes variables column - wise to fulfi ll the output request.

Thus the matrix M has been taken down the fi rst column, then down the second. To get a data fi le of the same “ shape, ” it is necessary to add two format fi elds, and transpose the matrix so that M T is now a 2 × 4 matrix. The construct:

fprintf (FileId, ‘ %.2f %.2f\n ’ , M ’ );

will give an output fi le mydata.dat : 0.70 0.05

0.91 0.74 0.76 0.33 0.26 0.63

In addition to text fi les as discussed above, one may encounter binary data fi les. These are not viewable using text editors — they consist of “ raw ” 8 or 16 - bit quantities (usually), which are machine readable and not human readable. The exact representation depends on the central processing unit (CPU) being used, and there

c02.indd 33

c02.indd 33 4/13/2011 5:20:51 PM4/13/2011 5:20:51 PM

34 CHAPTER 2 MATLAB FOR SIGNAL PROCESSING

are different conventions in common use depending on the manufacturer. Integer representations may be converted between formats, but the situation for fl oating - point numbers is much more problematic.

For this reason, the Institution of Electrical and Electronic Engineers (IEEE) fl oating - point format is commonly used (IEEE fl oating - point numbers are discussed in detail in Section 3.4 ). If the need to read or write binary fi les arises, the appropri- ate source code methods will often be supplied with the data. If not, they must be written using a combination of fread() and fwrite() .

Consider the following, which opens a fi le for reading in binary mode using the IEEE fl oating - point format:

FileId = fopen (FileName, ‘ rb ’ , ‘ ieee −le ’ );

if ( FileId < 0 )

fprintf (1, ‘ Cannot access %s\n ’ , FileName);

error ( ‘ myfunction() ’ );

end

ID = fread (FileId, 2, ‘ uchar ’ );

FileSize = fread (FileId, 1, ‘ ulong ’ );

The ieee - le indicates the use of the IEEE fl oating - point format, using “ little - endian ” byte ordering. This means that the least - signifi cant byte is read fi rst. Note the format specifi er ‘ rb’ (read - only, in binary mode) as opposed to the text fi les discussed previously, which were opened using ‘ r’ mode (text is the default). To read the fi le, instead of the fscanf () as before with appropriate formatting conver- sion specifi ers like %s for string, %d for numeric and so forth, we are restricted to intrinsic machine data types such as ‘ uchar ’ for an unsigned character (8 bit),

‘ ulong ’ for an unsigned long (32 bit) and so forth. To read two characters followed by one 32 - bit integer, we could use:

a = 321.456;

i8 = uint8 (a) i8 =

255

i16 = uint16 (a) i16 =

321

If the signal data fi le is stored in 8 - or 16 - bit binary format, then it may be necessary to use the above functions to read in the raw data. In that case, it is usually necessary to convert to fl oating point format using the double conversion function.

In the following, note how the number is truncated when converted to uint8 format (unsigned 8 - bit integer), and precision is lost when converted to uint16 format (unsigned 16 - bit integer).

c02.indd 34

c02.indd 34 4/13/2011 5:20:51 PM4/13/2011 5:20:51 PM

Một phần của tài liệu Digital signal processing using MATLAB for students and researchers john w leis (Trang 39 - 43)

Tải bản đầy đủ (PDF)

(386 trang)