Preparation of measurements
This commit is contained in:
		
							
								
								
									
										11
									
								
								matlab/frf_analyze.m
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								matlab/frf_analyze.m
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
			
		||||
% Analysis
 | 
			
		||||
% :PROPERTIES:
 | 
			
		||||
% :header-args: :tangle matlab/frf_analyze.m
 | 
			
		||||
% :END:
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
%% Load all the measurements
 | 
			
		||||
meas_data = {};
 | 
			
		||||
for i = 1:7
 | 
			
		||||
    meas_data(i) = {load(sprintf('mat/frf_data_%i.mat', i), 't', 'Va', 'Vs', 'd')};
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										32
									
								
								matlab/frf_save.m
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								matlab/frf_save.m
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
			
		||||
% Save Data
 | 
			
		||||
% :PROPERTIES:
 | 
			
		||||
% :header-args: :tangle matlab/frf_save.m
 | 
			
		||||
% :END:
 | 
			
		||||
 | 
			
		||||
% First, we get data from the Speedgoat:
 | 
			
		||||
 | 
			
		||||
tg = slrt;
 | 
			
		||||
 | 
			
		||||
f = SimulinkRealTime.openFTP(tg);
 | 
			
		||||
mget(f, 'data/data.dat');
 | 
			
		||||
close(f);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
% And we load the data on the Workspace:
 | 
			
		||||
 | 
			
		||||
data = SimulinkRealTime.utils.getFileScopeData('data/data.dat').data;
 | 
			
		||||
 | 
			
		||||
Va = data(:, 1); % Excitation Voltage (input of PD200) [V]
 | 
			
		||||
Vs = data(:, 2); % Measured voltage (force sensor) [V]
 | 
			
		||||
de = data(:, 3); % Measurment displacement (encoder) [m]
 | 
			
		||||
da = data(:, 4); % Measurement displacement (attocube) [m]
 | 
			
		||||
t  = data(:, end); % Time [s]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
% And we save this to a =mat= file:
 | 
			
		||||
 | 
			
		||||
apa_number = 1;
 | 
			
		||||
 | 
			
		||||
save(sprintf('mat/frf_data_%i.mat', apa_number), 't', 'Va', 'Vs', 'de', 'da');
 | 
			
		||||
							
								
								
									
										43
									
								
								matlab/frf_setup.m
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								matlab/frf_setup.m
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,43 @@
 | 
			
		||||
Fs = 10e3; % Sampling Frequency [Hz]
 | 
			
		||||
Ts = 1/Fs; % Sampling Time [s]
 | 
			
		||||
 | 
			
		||||
Tsim = 110; % Simulation Time [s]
 | 
			
		||||
 | 
			
		||||
Trec_start = 5;  % Start time for Recording [s]
 | 
			
		||||
Trec_dur   = 100; % Recording Duration [s]
 | 
			
		||||
 | 
			
		||||
%% Sweep Sine
 | 
			
		||||
V_sweep = generateSweepExc('Ts',      Ts, ...
 | 
			
		||||
                           'f_start', 10, ...
 | 
			
		||||
                           'f_end',   1e3, ...
 | 
			
		||||
                           'V_mean',  3.25, ...
 | 
			
		||||
                           't_start', Trec_start, ...
 | 
			
		||||
                           'exc_duration', Trec_dur, ...
 | 
			
		||||
                           'sweep_type',   'log', ...
 | 
			
		||||
                           'V_exc', 0.5/(1 + s/2/pi/100));
 | 
			
		||||
 | 
			
		||||
%% Shaped Noise
 | 
			
		||||
V_noise = generateShapedNoise('Ts', 1/Fs, ...
 | 
			
		||||
                              'V_mean', 3.25, ...
 | 
			
		||||
                              't_start', Trec_start, ...
 | 
			
		||||
                              'exc_duration', Trec_dur, ...
 | 
			
		||||
                              'smooth_ends', true, ...
 | 
			
		||||
                              'V_exc', 0.05/(1 + s/2/pi/10));
 | 
			
		||||
 | 
			
		||||
%% Select the excitation signal
 | 
			
		||||
V_exc = V_noise;
 | 
			
		||||
 | 
			
		||||
figure;
 | 
			
		||||
tiledlayout(1, 2, 'TileSpacing', 'Normal', 'Padding', 'None');
 | 
			
		||||
 | 
			
		||||
ax1 = nexttile;
 | 
			
		||||
plot(V_exc(1,:), V_exc(2,:));
 | 
			
		||||
xlabel('Time [s]'); ylabel('Amplitude [V]');
 | 
			
		||||
 | 
			
		||||
ax2 = nexttile;
 | 
			
		||||
win = hanning(floor(length(V_exc)/8));
 | 
			
		||||
[pxx, f] = pwelch(V_exc(2,:), win, 0, [], Fs);
 | 
			
		||||
plot(f, pxx)
 | 
			
		||||
xlabel('Frequency [Hz]'); ylabel('Power Spectral Density [$V^2/Hz$]');
 | 
			
		||||
set(gca, 'xscale', 'log'); set(gca, 'yscale', 'log');
 | 
			
		||||
xlim([1, Fs/2]); ylim([1e-10, 1e0]);
 | 
			
		||||
							
								
								
									
										53
									
								
								matlab/src/generateShapedNoise.m
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								matlab/src/generateShapedNoise.m
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,53 @@
 | 
			
		||||
function [U_exc] = generateShapedNoise(args)
 | 
			
		||||
% generateShapedNoise - Generate a Shaped Noise excitation signal
 | 
			
		||||
%
 | 
			
		||||
% Syntax: [U_exc] = generateShapedNoise(args)
 | 
			
		||||
%
 | 
			
		||||
% Inputs:
 | 
			
		||||
%    - args - Optinal arguments:
 | 
			
		||||
%        - Ts              - Sampling Time                                              - [s]
 | 
			
		||||
%        - V_mean          - Mean value of the excitation voltage                       - [V]
 | 
			
		||||
%        - V_exc           - Excitation Amplitude, could be numeric or TF               - [V rms]
 | 
			
		||||
%        - t_start         - Time at which the noise begins                             - [s]
 | 
			
		||||
%        - exc_duration    - Duration of the noise                                      - [s]
 | 
			
		||||
%        - smooth_ends     - 'true' or 'false': smooth transition between 0 and V_mean  - [-]
 | 
			
		||||
 | 
			
		||||
arguments
 | 
			
		||||
    args.Ts              (1,1) double  {mustBeNumeric, mustBePositive} = 1e-4
 | 
			
		||||
    args.V_mean          (1,1) double  {mustBeNumeric} = 0
 | 
			
		||||
    args.V_exc                                         = 1
 | 
			
		||||
    args.t_start         (1,1) double  {mustBeNumeric, mustBePositive} = 5
 | 
			
		||||
    args.exc_duration    (1,1) double  {mustBeNumeric, mustBePositive} = 10
 | 
			
		||||
    args.smooth_ends           logical {mustBeNumericOrLogical} = true
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
t_noise = 0:args.Ts:args.exc_duration;
 | 
			
		||||
 | 
			
		||||
if isnumeric(args.V_exc)
 | 
			
		||||
    V_noise = args.V_mean + args.V_exc*sqrt(1/args.Ts/2)*randn(length(t_noise), 1)';
 | 
			
		||||
elseif isct(args.V_exc)
 | 
			
		||||
    V_noise = args.V_mean + lsim(args.V_exc, sqrt(1/args.Ts/2)*randn(length(t_noise), 1), t_noise)';
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
t_smooth_start = args.Ts:args.Ts:args.t_start;
 | 
			
		||||
 | 
			
		||||
V_smooth_start = zeros(size(t_smooth_start));
 | 
			
		||||
V_smooth_end   = zeros(size(t_smooth_start));
 | 
			
		||||
 | 
			
		||||
if args.smooth_ends
 | 
			
		||||
    Vd_max = args.V_mean/(0.7*args.t_start);
 | 
			
		||||
 | 
			
		||||
    V_d = zeros(size(t_smooth_start));
 | 
			
		||||
    V_d(t_smooth_start < 0.2*args.t_start) = t_smooth_start(t_smooth_start < 0.2*args.t_start)*Vd_max/(0.2*args.t_start);
 | 
			
		||||
    V_d(t_smooth_start > 0.2*args.t_start & t_smooth_start < 0.7*args.t_start) = Vd_max;
 | 
			
		||||
    V_d(t_smooth_start > 0.7*args.t_start & t_smooth_start < 0.9*args.t_start) = Vd_max - (t_smooth_start(t_smooth_start > 0.7*args.t_start & t_smooth_start < 0.9*args.t_start) - 0.7*args.t_start)*Vd_max/(0.2*args.t_start);
 | 
			
		||||
 | 
			
		||||
    V_smooth_start = cumtrapz(V_d)*args.Ts;
 | 
			
		||||
 | 
			
		||||
    V_smooth_end = args.V_mean - V_smooth_start;
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
V_exc = [V_smooth_start, V_noise, V_smooth_end];
 | 
			
		||||
t_exc = args.Ts*[0:1:length(V_exc)-1];
 | 
			
		||||
 | 
			
		||||
U_exc = [t_exc; V_exc];
 | 
			
		||||
							
								
								
									
										76
									
								
								matlab/src/generateSweepExc.m
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										76
									
								
								matlab/src/generateSweepExc.m
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,76 @@
 | 
			
		||||
function [U_exc] = generateSweepExc(args)
 | 
			
		||||
% generateSweepExc - Generate a Sweep Sine excitation signal
 | 
			
		||||
%
 | 
			
		||||
% Syntax: [U_exc] = generateSweepExc(args)
 | 
			
		||||
%
 | 
			
		||||
% Inputs:
 | 
			
		||||
%    - args - Optinal arguments:
 | 
			
		||||
%        - Ts              - Sampling Time                                              - [s]
 | 
			
		||||
%        - f_start         - Start frequency of the sweep                               - [Hz]
 | 
			
		||||
%        - f_end           - End frequency of the sweep                                 - [Hz]
 | 
			
		||||
%        - V_mean          - Mean value of the excitation voltage                       - [V]
 | 
			
		||||
%        - V_exc           - Excitation Amplitude for the Sweep, could be numeric or TF - [V]
 | 
			
		||||
%        - t_start         - Time at which the sweep begins                             - [s]
 | 
			
		||||
%        - exc_duration    - Duration of the sweep                                      - [s]
 | 
			
		||||
%        - sweep_type      - 'logarithmic' or 'linear'                                  - [-]
 | 
			
		||||
%        - smooth_ends     - 'true' or 'false': smooth transition between 0 and V_mean  - [-]
 | 
			
		||||
 | 
			
		||||
arguments
 | 
			
		||||
    args.Ts              (1,1) double  {mustBeNumeric, mustBePositive} = 1e-4
 | 
			
		||||
    args.f_start         (1,1) double  {mustBeNumeric, mustBePositive} = 1
 | 
			
		||||
    args.f_end           (1,1) double  {mustBeNumeric, mustBePositive} = 1e3
 | 
			
		||||
    args.V_mean          (1,1) double  {mustBeNumeric} = 0
 | 
			
		||||
    args.V_exc                                         = 1
 | 
			
		||||
    args.t_start         (1,1) double  {mustBeNumeric, mustBeNonnegative} = 5
 | 
			
		||||
    args.exc_duration    (1,1) double  {mustBeNumeric, mustBePositive} = 10
 | 
			
		||||
    args.sweep_type            char    {mustBeMember(args.sweep_type,{'log', 'lin'})} = 'lin'
 | 
			
		||||
    args.smooth_ends           logical {mustBeNumericOrLogical} = true
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
t_sweep = 0:args.Ts:args.exc_duration;
 | 
			
		||||
 | 
			
		||||
if strcmp(args.sweep_type, 'log')
 | 
			
		||||
    V_exc = sin(2*pi*args.f_start * args.exc_duration/log(args.f_end/args.f_start) * (exp(log(args.f_end/args.f_start)*t_sweep/args.exc_duration) - 1));
 | 
			
		||||
elseif strcmp(args.sweep_type, 'lin')
 | 
			
		||||
    V_exc = sin(2*pi*(args.f_start + (args.f_end - args.f_start)/2/args.exc_duration*t_sweep).*t_sweep);
 | 
			
		||||
else
 | 
			
		||||
    error('sweep_type should either be equal to "log" or to "lin"');
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
if isnumeric(args.V_exc)
 | 
			
		||||
    V_sweep = args.V_mean + args.V_exc*V_exc;
 | 
			
		||||
elseif isct(args.V_exc)
 | 
			
		||||
    if strcmp(args.sweep_type, 'log')
 | 
			
		||||
        V_sweep = args.V_mean + abs(squeeze(freqresp(args.V_exc, args.f_start*(args.f_end/args.f_start).^(t_sweep/args.exc_duration), 'Hz')))'.*V_exc;
 | 
			
		||||
    elseif strcmp(args.sweep_type, 'lin')
 | 
			
		||||
        V_sweep = args.V_mean + abs(squeeze(freqresp(args.V_exc, args.f_start+(args.f_end-args.f_start)/args.exc_duration*t_sweep, 'Hz')))'.*V_exc;
 | 
			
		||||
    end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
if args.t_start > 0
 | 
			
		||||
    t_smooth_start = args.Ts:args.Ts:args.t_start;
 | 
			
		||||
 | 
			
		||||
    V_smooth_start = zeros(size(t_smooth_start));
 | 
			
		||||
    V_smooth_end   = zeros(size(t_smooth_start));
 | 
			
		||||
 | 
			
		||||
    if args.smooth_ends
 | 
			
		||||
        Vd_max = args.V_mean/(0.7*args.t_start);
 | 
			
		||||
 | 
			
		||||
        V_d = zeros(size(t_smooth_start));
 | 
			
		||||
        V_d(t_smooth_start < 0.2*args.t_start) = t_smooth_start(t_smooth_start < 0.2*args.t_start)*Vd_max/(0.2*args.t_start);
 | 
			
		||||
        V_d(t_smooth_start > 0.2*args.t_start & t_smooth_start < 0.7*args.t_start) = Vd_max;
 | 
			
		||||
        V_d(t_smooth_start > 0.7*args.t_start & t_smooth_start < 0.9*args.t_start) = Vd_max - (t_smooth_start(t_smooth_start > 0.7*args.t_start & t_smooth_start < 0.9*args.t_start) - 0.7*args.t_start)*Vd_max/(0.2*args.t_start);
 | 
			
		||||
 | 
			
		||||
        V_smooth_start = cumtrapz(V_d)*args.Ts;
 | 
			
		||||
 | 
			
		||||
        V_smooth_end = args.V_mean - V_smooth_start;
 | 
			
		||||
    end
 | 
			
		||||
else
 | 
			
		||||
    V_smooth_start = [];
 | 
			
		||||
    V_smooth_end = [];
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
V_exc = [V_smooth_start, V_sweep, V_smooth_end];
 | 
			
		||||
t_exc = args.Ts*[0:1:length(V_exc)-1];
 | 
			
		||||
 | 
			
		||||
U_exc = [t_exc; V_exc];
 | 
			
		||||
		Reference in New Issue
	
	Block a user