close all; clear all; 

Array=readmatrix('C:\Users\Stefan Reinelt\Documents\Messtechnik Hiwi\SENSORIK Praktikum\high_res_spul_y.log');

mx = Array(:, 1); 
my = Array(:, 2);
mz = Array(:, 3);

r_mx = Array(:, 4);
r_my = Array(:, 5);
r_mz = Array(:, 6);
time = Array(:, 7);
disp(time(1))
for x =time
    timeN = (x - time(1));
    timeN = timeN/1000;
end

figure('Name', 'norm and raw')
tiledlayout(2,2)
nexttile
plot( timeN, mx)
hold on 
plot(timeN, r_mx)
title("x-component")
legend("norm", "raw")
xlabel('time in s');
ylabel('magnetic field in \muT');

nexttile 
plot( timeN, my)
hold on 
plot(timeN, r_my)
title("y-component")
xlabel('time in s');
ylabel('magnetic field in \muT');

nexttile 
plot( timeN, mz)
hold on 
plot(timeN, r_mz)
title("z-component")
xlabel('time in s');
ylabel('magnetic field in \muT');

figure('Name', ' raw')
tiledlayout(2,2)
nexttile
%plot( timeN, mx)
hold on 
plot(timeN, r_mx)
title("x-component")
% legend("norm", "raw")
xlabel('time in s');
ylabel('magnetic field in \muT');

nexttile 
%plot( timeN, my)
hold on 
plot(timeN, r_my)
title("y-component")
xlabel('time in s');
ylabel('magnetic field in \muT');

nexttile 
%plot( timeN, mz)
hold on 
plot(timeN, r_mz)
title("z-component")
xlabel('time in s');
ylabel('magnetic field in \muT');

%% Fourier Transform of Signal to analyze it
M = mean(mx) 
mx = mx-M;

M = mean(my) 
my = my-M;

M = mean(mz)
mz = mz-M;
timeN = timeN(:);
Fs = 1000; %sampling frequenz
T = 1/Fs; %sampling period
L = length(timeN); %Length of signal
t = (0:L-1)*T; % time vector

mz_freq = fft(mz);
mx_freq = fft(mx);
my_freq = fft(my);
P2z = abs(mz_freq/L);
P2y = abs(my_freq/L);
P2x = abs(mx_freq/L);
P1z = P2z(1:L/2+1);
P1y = P2y(1:L/2+1);
P1x = P2x(1:L/2+1);
P1z(2:end-1) = 2*P1z(2:end-1);
P1y(2:end-1) = 2*P1y(2:end-1);
P1x(2:end-1) = 2*P1x(2:end-1);
f = Fs*(0:(L/2))/L;
figure();
plot(f, P1z); %single sided amplitude spectrum of Magnetic field in z
hold on
plot(f, P1x);
hold on
plot(f, P1y);
legend("mz", "mx", "my");
title("Single sided amplitude spectrum of mz"); 
xlabel("f in Hz"); 
ylabel("|P1(f)|"); 

[ma, pos] = max(P1z);
max_mz = f(pos)
[ma, pos] = max(P1x);
max_mx = f(pos)
[ma, pos] = max(P1y);
max_my = f(pos)

%% Calibration of Magnetic Sensor
%first chip

true_z = [30; 160; 260; 390; 520; 660; 780; 840; 1020; 1100; 1300; 1480; 1500; 1630; 1730; 1900; 2040];
meas_z_01 = [500; 2420; 4337; 6260; 8160; 10070; 11960; 13830; 15700; 17540; 19420; 21380; 23452; 25580; 27600; 29660; 31700];
cal_z_01 = meas_z_01./true_z;
cal_z_01_mean = mean(cal_z_01)
cal_z_01_std = std(cal_z_01)
lin_fit_z = polyfit(true_z, meas_z_01, 1);
fit_z = polyval(lin_fit_z, true_z);

true_y = [30; 140; 260; 380; 500; 740; 760; 880; 1000];
meas_y_01 = [3370; 5400; 7400; 9400; 11370; 13345; 15300; 17260; 19180];
cal_y_01 = meas_y_01./true_y;
cal_y_01_mean = mean(cal_y_01)
cal_y_01_std = std(cal_y_01)
lin_fit_y = polyfit(true_y, meas_y_01, 1);
fit_y = polyval(lin_fit_y, true_y);


figure();
subplot(2,1,1);
plot(true_z, meas_z_01, "x", "LineWidth", 2);
hold on;
plot(true_z, fit_z, "--r", "LineWidth", 2);
legend("Measured values", "linear fit", "Location", "southeast");
box on; grid on;
xlabel("Feldstärke in µT");
ylabel("gemessene Feldstärke in a.u.");
eqn_z = "Linear; y = " + string(lin_fit_z(1)) + " x + " + string(lin_fit_z(2));
text(min(true_z), max(meas_z_01), eqn_z, "HorizontalAlignment", "left", "VerticalAlignment", "top");
title("Feld in z Richtung");

subplot(2,1,2);
plot(true_y, meas_y_01, "x", "Linewidth", 2);
hold on;
plot(true_y, fit_y, "--r", "LineWidth", 2);
legend("Measured values", "linear fit", "Location", "southeast");
box on; grid on;
xlabel("Feldstärke in µT");
ylabel("gemessene Feldstärke in a.u.");
eqn_y = "Linear; y = " + string(lin_fit_y(1)) + " x + " + string(lin_fit_y(2));
text(min(true_y), max(meas_y_01), eqn_y, "HorizontalAlignment", "left", "VerticalAlignment", "top");
title("Feld in y Richtung");


%% Magnet Frequenzgang
% Import data from text file
% Script for importing data from the following text file:
%
%    filename: C:\Users\Stefan Reinelt\Documents\Messtechnik Hiwi\SENSORIK Praktikum\serial_readout_magnet_freq_nicla_01.csv
%
% Auto-generated by MATLAB on 11-Jul-2022 16:49:54

% Set up the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 2);

% Specify range and delimiter
opts.DataLines = [2, Inf];
opts.Delimiter = ";";

% Specify column names and types
opts.VariableNames = ["PuTTYLog20220711164756", "VarName2"];
opts.VariableTypes = ["double", "double"];

% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";

% Import the data
serialreadoutmagnetfreqnicla01 = readtable("C:\Users\Stefan Reinelt\Documents\Messtechnik Hiwi\SENSORIK Praktikum\serial_readout_magnet_freq_nicla_01.csv", opts);


% Clear temporary variables
clear opts

data_freq = table2array(serialreadoutmagnetfreqnicla01);
z_raw = data_freq(:, 1); 
bad = find(isnan(z_raw))
%bad = 1; %nur falls kein bad gefunden wird
z_raw = z_raw(bad+1:end);
Fs = 1000; %sampling frequenz
T = 1/Fs; %sampling period
L = length(z_raw); %Length of signal
t = (0:L-1)*T; % time vector

freq_z_raw = fft(z_raw);
P2 = abs(freq_z_raw/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
f = Fs*(0:(L/2))/L;

figure();
subplot(2, 1, 1);
plot(t, z_raw);
box on; grid on; 
xlabel("Time in s");
ylabel("z_raw in a.u.");

subplot(2,1,2);
plot(f(2:end), P1(2:end));
box on; grid on;
xlabel("f in Hz");
ylabel("|P1(f)|"); 

[M, pos] = max(P1(2:end))
max_f = f(pos)

%for measurement 01: f = 1 Hz, gemessenes max bei 0 bzw 13.75 Hz
%for measurement 02: f = 10 Hz, gemessenes max bei 0 bzw 24.34 Hz
%for measurement 03: f = 20 Hz, gemessenes max bei 0 bzw 22.8 Hz
