#!/usr/bin/perl -w use strict; ############################################################################### # wsr2csv.pl - Converts Wi-Spy Recordings to comma-separated value (csv) files # Copyright (C) 2006 MetaGeek, LLC # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # # http://www.gnu.org/copyleft/gpl.html # ############################################################################### my($HEADER_SIZE) = 8816; my($NUM_FREQUENCIES) = 85; my($inputFileName); my($outputFileName); # Set the input and output file names my($numArgs) = $#ARGV + 1; if ($numArgs >= 1) { $inputFileName = $ARGV[0]; } else { &Dienice("Please specify input file as first parameter"); } if ($numArgs >= 2) { $outputFileName = $ARGV[1]; } else { (my($fileName), my($ext)) = split(/\./, $inputFileName); $outputFileName = "$fileName.csv"; } # Open the input file open(INPUT, "$inputFileName") or &Dienice("Couldn't open $inputFileName for reading"); # Set input file I/O mode to binary binmode(INPUT); # Open the output file open(OUTPUT, ">$outputFileName") or &Dienice("Couldn't open $outputFileName for writing"); # Read the header from input file my($readcount) = read(INPUT, my($inputData), $HEADER_SIZE); print "Converting $inputFileName\n"; print OUTPUT "Timestamp,Seconds,"; # Print header row for (my($frequency) = 2400; $frequency < $NUM_FREQUENCIES + 2400; $frequency++) { print OUTPUT "$frequency"; if ($frequency != $NUM_FREQUENCIES + 2400 - 1) { print OUTPUT ","; } } print OUTPUT "\n"; my($moreData) = 1; while ($moreData == 1) { # Read timestamp $readcount = read(INPUT, my($timestamp), 4); my($output) = unpack('N', $timestamp); print OUTPUT "$output,"; $readcount = read(INPUT, $timestamp, 4); $output = unpack('N', $timestamp); print OUTPUT "$output,"; if ($readcount == 4) { for (my($frequency) = 0; $frequency < $NUM_FREQUENCIES; $frequency++) { # Read amplitude of frequency $readcount = read(INPUT, my($amplitude), 1); if ($readcount == 1) { # Interpret $amplitude as signed char my($output) = unpack('c', $amplitude); # Print amplitude to output file print OUTPUT "$output"; if ($frequency != $NUM_FREQUENCIES - 1) { print OUTPUT ","; } else { print OUTPUT "\n"; } } else { $moreData = 0; } } } else { $moreData = 0; } } close(INPUT); close(OUTPUT); print "Successfully converted $inputFileName to $outputFileName"; ########################################### sub Dienice { my($errmsg) = @_; print "Error: $errmsg\n"; exit; }