%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  NOME SCRIPT...ltspice.sanity.check.m 
%  VERSIONE......1.0 
%  DATA..........Novembre 2016
%
%
% INFO
%  Una volta esportati i valori (Tensione e Corrente) della simulazione LTspice di un circuito
%  in un file .csv, lo script stampa il valore minimo, massimo, RMS di ciascun parametro.
%
% USE
%
%   1) IN LTSPICE: FILE -> EXPORT -> SELECT TRACES TO EXPORT
%
%   2) CONVERT FROM TAB-SEPARATED VALUES TO COMMA SEPARATED VALUES WITH THE SCRIPT:
%       $ ./tsv2csv.sh <NOMEfile>
%
%   3) IN OCTAVE/MATLAB: ltspice.sanity.check.m (SET VARIABLES BELOW FIRST!)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



clear all
fflush (stdout);
clc;



% By default installed packages are not available from the Octave prompt, but it is possible to control 
% this using the pkg load and pkg unload commands. The functions from a package can be added to the 
% Octave path by typing :
%   pkg load package_name
% use: 
%   pkg list
% to list all intalled packages (an asterisk indicates the package is already loaded)
% 
pkg load io   % for csv2cell() function






%
%  eng: displays the value using an engineering format, where the exponent is divisible by 3
%
format short eng 



% numero di dati tabulati su una riga
global SETTORI = 5;


% selezione output:
% "terminale" per stampa a video oppure "<nomefile>" per stampa su file 
OutputDevice="terminale";



% legge il file .csv interattivamente
%global nomefile=input ("Inserisci il nome del file da leggere :   ", "s");
% not working...
%system(tsv2csv.sh nomefile)
%data_array=csv2cell(nomefile);



% OPPURE...

% legge il file .csv
data_array=csv2cell('Buck_Igen_MAXVALUES.csv');


fprintf ("   *******************************************************************************\n")
fprintf ("\n")
fprintf ("   LTspice sanity check\n")
fprintf ("   --------------------\n")
fprintf ("\n")
fprintf ("   Numero elementi : %g\n", columns(data_array))
fprintf ("   Numero misurazioni per ciascun elemento : %g\n", rows(data_array))
fprintf ("\n")
fprintf ("   *******************************************************************************\n")
       
       





%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%     FUNCTION rms2 
%
%   RMS Compute the Root Mean Square value of the input vector
%   This function computes the RMS value using a bit more of a brute-force method
%
% function [out] = rms2(in)
%   out = sqrt(sum((in.^2)/max(size(in))));
% end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%













%  data_array

%  The data obtained indexing Cell Arrays like c{2,3} are string.
%  If you want convert the value (int, double), please see the octave manual
%  5.6 String Conversions str2num may be  useful function.

% OUTPUT
% 0 1 2 3 4 5 6
% x x x x x x x
% y y y y y y y 
% z z z z z z z
%
% where
% 0 = time
% 1,2... = I(R1:1) or V(C4:1)
% x,y,z values of time (seconds), voltage (V) or current(A) in engineering notation
%
%
%










% crea un array con i nomi delle variabili del file
% cioe la prima riga del cell array 'data_array'
nome_array=data_array(1, :);



% rimuove la prima RIGA del cell array 'data_array' che conteneva i nomi,
% questo per avare un array di soli numeri:
%  For unconvertible scalar or character string input str2double returns a NaN. Similarly, 
%  for character array input str2double returns a NaN for any row of s that could not be converted. 
%  For a cell array, str2double returns a NaN for any element of s for which conversion fails. 
%  Note that numeric elements in a mixed string/numeric cell array are not strings and the 
%  conversion will fail for these elements and return NaN.
data_array(1, :) = [];




% converte il 'cell array' in matrice numerica
num_data=str2double(data_array);




global righe=rows (num_data);
global colonne=columns (num_data);




for col = 1:colonne


%NOME
 disp_array{1,col} = nome_array{1,col};
par_name{1,1} = "";

%Min
 disp_array{2,col} = min (num_data(1:righe,col));
 par_name{2,1} = "MIN";


%Max
 disp_array{3,col} = max (num_data(1:righe,col));
 par_name{3,1} = "MAX";


%Media
 disp_array{4,col} = mean(num_data(1:righe,col));
  par_name{4,1} = "MED";


%Varianza
 %disp_array{6,col} = std(num_data(1:righe,col));
 %par_name{6,1} = "VAR";


%RMS
% disp_array{5,col} =  sqrt(sum(((num_data(1:righe,col)).^2)/max(size(num_data(1:righe,col))))) ;
 disp_array{5,col} =  RootMeanSquare(num_data(1:righe,col));

 par_name{5,1} = "RMS";


endfor














% StampaSettori (disp_array, par_name, outdevice, num_settori)
%
%
% VARIABILI PASSATE COME PARAMETRO
% --------------------------------
% disp_array  : array contenete in ogni riga i valori dei parametri
%               es a1 a2 a3 a4 a5 <-- valori del parametro a
%               b1 b2 b3 b4 b5 <-- valori del parametro b 
%
% par_name    : array contenete i nomi dei parametri = num righe di disp_array
%               es: min, max
%
% outdevice   : inserire "terminale" per stampare a terminale, "nomefile" per scrivere sul file nomefile
%
% num_settori : numero di settori da tabulare per ogni riga

StampaSettori (disp_array, par_name, OutputDevice, 6)
printf ("\n")



% Visualizza tutte le variabili in memoria
%whos

% Rimuove tutte le variabili del programma dalla memoria
%clear all











