function savewave(wavefile,waveData,sRate)
%SAVEWAVE Saves Microsoft Windows 3.1 .WAV format sound files.
%   SAVEWAVE(wavefile,y,Fs) saves a .WAV format file specified by "wavefile".
%
%   The input arguments for SAVEWAVE are as follows:
%
%       wavefile    A string containing the name of the .WAV file to create
%       y           The sampled data to save (8 bit max)
%       Fs          The rate at which the data was sampled
%               
%   Note: SAVEWAVE will create an 8-bit, simgle channel wave file. Non 8-bit
%   sample data will be truncated.
%
%   See also, LOADWAVE .

%       Copyright (c) 1984-93 by The MathWorks, Inc.

if nargin~=3
	error('SAVEWAVE needs three arguments!');
end

if findstr(wavefile,'.')==[]
	wavefile=[wavefile,'.wav'];
end

fid=fopen(wavefile,'wb');

if fid ~= -1
	[m,n]=size(waveData);
	nsamples=m*n;

	riffsize=36+nsamples;

	% write riff chunk
	fwrite(fid,'RIFF','uchar');
	fwrite(fid,riffsize,'ulong');
	fwrite(fid,'WAVE','uchar');

	% write format sub-chunk
	fwrite(fid,'fmt ','uchar');
	fwrite(fid,16,'ulong');

	fwrite(fid,1,'ushort');         % PCM format 
	fwrite(fid,1,'ushort');         % 1 channel
	fwrite(fid,sRate,'ulong');      % samples per second
	fwrite(fid,sRate,'ulong');      % average bytes per second
	fwrite(fid,1,'ushort');         % block alignment
	fwrite(fid,8,'ushort');         % bits per sample


	% write data sub-chunck
	fwrite(fid,'data','uchar');
	fwrite(fid,nsamples,'ulong');
	fwrite(fid,waveData,'uchar');

	if ((ceil(nsamples/2))~=(nsamples/2)) 
		fwrite(fid,0,'uchar');
	fclose(fid);
	end
end;

if fid == -1
	error('Can''t open .WAV file for input!');
end;
