%% Script per visualizzare segnali nel dominio del tempo e delle frequenze

close all
clear all
clc

%% Struttura dei file di input:
% tempo [s]
% channel 1(opz): funzione di moto imposta [V]
% channel 2: spostamento tip laser [V]
% channel 3: accelerometro su shaker [V]
% channel 4: harvester [V]

%sensibilitą 
laser_sens= 2; %V/mm
acc_sens= 100e-3/9.81; %V/(m/s^2)

%numero del file .csv da cui leggere il segnale
kk=2;

%canale del segnale (colonna, tempo incluso)
ii=4;

%caricamento dati
dati=importdata(['dati/scope_' int2str(kk) '.csv'],',',2);

tempo=dati.data(:,1) - dati.data(1,1); %tempo traslato allo zero
segnale=dati.data(:,ii);


%% Analisi in frequenza
N=length(segnale);
dt=tempo(2);
fsamp=1/dt;
df=fsamp/N;

freq=0:df:fsamp/2;
    
% trasformata
TF=fft(segnale)/N;
modulo=abs(TF(1:N/2+1))*2;
modulo(1)=abs(TF(1));
modulo(N/2+1)=abs(TF(N/2+1));

fase=angle(TF(1:N/2+1));

%% Plot
figure('position',[100,100,600,400])
subplot(311)
plot(tempo,segnale,'m','linewidth',1)
xlabel('tempo [s]')
title('-------storia temporale-------')
grid
subplot(312)
plot(freq,modulo,'m','linewidth',1)
grid
xlim([0 300])
xlabel('freq [Hz]')
ylabel('modulo spettro')
title('-------FFT------')
subplot(313)
plot(freq,fase,'m','linewidth',1)
xlim([0 300])
xlabel('freq [Hz]')
ylabel('fase spettro')
grid


%% salvataggio EPS
% print -painters -depsc img/spettro_segnale.eps
print -painters -dpng img/spettro_segnale.png
