#!/usr/bin/perl # file: samples_to_mat.pm G. Moody 5 March 2009 # Last revised: 11 February 2014 # _____________________________________________________________________________ # samples_to_mat module for PhysioBank's Automated Teller Machine # Copyright (C) 2009-2014 George B. Moody use File::Basename; sub mod_samples_to_mat { # Set the path, URL, and final output filenames and URLs for this request. $wd = "export/matlab"; $wu = "$baseurl/$wd"; $wp = "$basepath/$wd"; $orec = basename($record); $orec =~ tr/./_/; $info = "$orec" . "m.info"; $mat = "$orec" . "m.mat"; $hea = "$orec" . "m.hea"; $pinfo = "$wp/$info"; $pmat = "$wp/$mat"; $phea = "$wp/$hea"; $uinfo = "$wu/$info"; $umat = "$wu/$mat"; $uhea = "$wu/$hea"; # Set up the command for this request. @w2m_args = ("-r", "$database/$record", "-f", $tstart, "-t", $tend, "-l", "s1000000"); if (defined $signal_num) { push(@w2m_args, "-s", $signal_num); } # If results for this request are already in the cache, return them. if (-s $pinfo && -s $pmat && -s $phea) { mod_mat_out(); return; } # Create the scratch directory for this request. $wt = "/ptmp/atm/scratch/$$/"; mkpath($wt); # Make temporary filenames. $tinfo = "$wt/$info"; $tmat = "$wt/$mat"; $thea = "$wt/$hea"; unless (fork) { chdir $wt; open(STDOUT, ">$tinfo"); exec($WFDB2MAT, @w2m_args); } # Wait until wfdb2mat is finished. wait; if (-s $phea) { # there was a race, and this process lost! unlink($tinfo); unlink($tmat); unlink($thea); # cron will remove $wt } else { # make sure path to $pmat exists ($record might include path info) mkpath("$wp/$record"); rename($tinfo, $pinfo); rename($tmat, $pmat); rename($thea, $phea); rmdir("$wp/$record"); # safe since files are kept one level above this } mod_mat_out(); } sub mod_mat_out { # Show the command and the info. if (-s $pinfo) { # if ($signal =~ / /) { pop(@w2m_args); push(@w2m_args, "'$signal'"); } $tmsize = -s $pmat; $thsize = -s $phea; $tisize = -s $pinfo; $mrec = $orec . 'm'; print 'The output below was prepared using this', ' command:
wfdb2mat' .
	    " @w2m_args", " >' .
	    "$mrec.info",
	    '
'; print "

Download these files:" . "

" . "

How to read these files in MATLAB or Octave:
" . " Download both $mrec.mat and $mrec.info. Also" . " download plotATM.m if you have not done so previously." . "

Each row of $mrec.mat contains the samples of one" . " signal. Each column contains a sample of each" . " signal observed at the same time. The time intervals between" . " consecutive columns are equal and specified in $mrec.info." . "

In MATLAB or Octave, run the command

" .
	      "   plotATM('$mrec')
" . " to view the signals. Inspect plotATM.m to see how use the" . " information from $mrec.info to convert the raw samples from" . " $mrec.mat into values in calibrated physical units.", "

Note: This is a conversion of signals only," . " not annotations. The amount of data converted is" . " limited to 1 million samples per signal since larger amounts" . " may be difficult to manipulate using MATLAB or Octave."; } else { show_html("no-samples"); } } 1;