###########################################################################
#                                                                         #
# Octave script to generate samples for two sinusoidal signals modulated  #
# by a triangular signal (sin_modulated_tri). Output signals are stored   #
# in two .csv file, input0.csv and input1.csv                             #
#                                                                         #
# Requirements:                                                           #
#   - octave                                                              #
#   - octave signal package                                               #
#                                                                         #
# In Debian GNU/Linux systems, both can be installed by issuing the       #
# following command:                                                      #
#   - sudo apt install octave octave-signal                               #
#                                                                         #
# How to run:                                                             #
#   - octave geninputs.m                                                  #
#                                                                         #
# Author:                                                                 #
#   - Hipólito Guzmán-Miranda (hguzman@us.es)                             #
#                                                                         #
###########################################################################

# Sample rate (Hz)
fs = 60000;

# Sampling interval
intvl = 1/fs;

# Total signal time
secs = 0.13;

# Time vector
time = intvl:intvl:secs;

# Signal frequency (Hz)
freq = 2000;

# Triangle frequency (Hz)
freq_tri = 100;

# Triangle duty cycle (between 0 and 1)
duty_tri = 0.5

# Load the signal processing package so we can use the pulstran function
pkg load signal

# Create two sinusoidal signals, one for each channel
amplitude = 619;
ch0 = amplitude*sin(time*2*pi*freq);
ch1 = amplitude*cos(time*2*pi*freq);

# Create a triangular signal
tri = 0.25*pulstran(time, intvl:1/freq_tri:secs, "tripuls", duty_tri/freq_tri)+0.25;

# Modulate sinusoidals with the triangular signal
ch0 = ch0.*tri;
ch1 = ch1.*tri;

# Apply floor operator and adjust offset if necessary
offset0 = 0;
offset1 = 0;
ch0 = floor(ch0) + offset0;
ch1 = floor(ch1) + offset1;

# Plot the generated signals and save the plot in a .png image
plot(time, ch0, time, ch1, time, tri)
legend('ch0', 'ch1', 'tri')
print -dpng 'stimuli.png'

# Print the actual range of the values for each channel
printf("Channel 0 in range: %d, %d\n", min(ch0), max(ch0))
printf("Channel 1 in range: %d, %d\n", min(ch0), max(ch0))

# Store generated signals in .csv files for the stimulation script
csvwrite("input0.csv",ch0')
csvwrite("input1.csv",ch1')

# Wait until the user closes the plots before exiting the script. If we don't
# do this, the plot windows will be automatically closed)
waitfor(gcf)
