% COSWAVE.M - Generates a cosine wave: 8000 samples, 1 s, 200 Hz
% (when played at 8000 samples/s)

  length = 8000;
  freq = 0.025;                % i.e. 200 Hz / 8000 Hz
  arg = 2 * pi * freq;

Out = round(32000 * cos(1:length *
arg));


% SIGNAL_OUT.M
% Write a signal in variable "Out" to a raw binary file

file_out = input('Please enter the name of the output signal file: ','s');
fid_out = fopen(file_out,'w');
fwrite(fid_out,Out,'integer*2');
status = fclose(fid_out);

% Convert a raw binary signal to a wav file
unix('sox -r 8000 -b 16 -e signed-integer -c 1 cosine.raw cosine.wav')

% ALTERNATIVELY, you could just use
% audiowrite (filename, y, sampling_rate, 'BitsPerSample', nbits_per_sample)

audiowrite ('cosine.wav', Out, 16000, 'BitsPerSample', 16)

% 16000 samples per second and 16 bits per sample is the default, so those do not need to be spelled out; it is sufficient to use

audiowrite ('cosine.wav', Out)

% Type 'help audioread' in Octave for more options