% Version 1.0
% Robin Hartley (PhD), University of Bristol, Bristol Composites, Institute, Bristol, United Kingdom
% - This is a function that analyses dynamic DSC data for glass
% transitions using a GUI which allows you to smooth the data
% interactively and interactively select the analysis regions using
% draggable verticle lines
%
% - Variables:
% Temperature - DSC temperature data (°C)
% DSCsignal - DSC data (mW/mg)
% - Outputs:
% TgOnset - Glass transition onset
% TgMid - Glass transition midpoint
%Here is a demonstration of how it works using a sin wave:
function [TgOnset,TgMid] = GlassTransitionFunc
% Define sine wave parameters
t = 0:0.01:1; % time vector
f = 1; % frequency of sine wave
A = 1; % amplitude of sine wave
sine_wave = A*sin(2*pi*f*t); % generate sine wave
% Add noise to sine wave
SNR = 25; % signal-to-noise ratio
noise_power = (norm(sine_wave)^2)/(length(sine_wave)*10^(SNR/10));
noise = sqrt(noise_power)*randn(size(sine_wave));
noisy_sine_wave = sine_wave + noise; % add noise to sine wave
Temp = t;
HeatFlow = noisy_sine_wave;
[TgOnset,TgMid] = TGLine(Temp,HeatFlow);
end
function [TgOnset,TgMid] = TGLine(xdata,ydata)
hLineToDrag = [];
smooth_ydata = sgolayfilt(ydata,3,11); %Set up initially smoothed y signal
%Create figure window
h_fig = figure;
h_fig.UserData.isButtonDown = false; %Create flag for mousedown event within figure
%Set up data associated with the figure handle
setappdata(h_fig,'xdata',xdata)
setappdata(h_fig,'ydata1',ydata)
setappdata(h_fig,'ydata2',smooth_ydata)
%Create axes window
h_ax1 = axes('unit','norm','Position',[0.1 0.22 0.85 0.75]);
%Select suitable axes limits and associate the figure handle
yrange = range(ydata);
xl = [min(xdata) max(xdata)];
yl = [min(ydata)-yrange*0.1 max(ydata)+yrange*0.1];
setappdata(h_fig,'xlims',xl);
setappdata(h_fig,'ylims',yl);
setappdata(h_fig,'axes_handle',h_ax1); %associate axes with figure handle
%Plot data and initialise the draggable vertical lines
updatePlot(h_fig,h_ax1);
initiateVertLines(h_fig,h_ax1)
%Creat button used to save the data and figure
FinalTgButton = uicontrol('units','normalized','Position',[0.65 0.05 0.2 0.05],'String','OKAY',...
'Callback', @FinaliseTgButton);
TgLinePlot(h_fig)
h_sld = uicontrol('Style', 'slider',...
'Min',5,'Max',length(ydata)*0.3,'Value',25,...
'unit','norm',...
'Tag','Slider',...
'Position', [0.05 0.05 0.4 0.05],...
'Callback', @slider_callback);
%
setappdata(h_sld,'xdata',xdata)
setappdata(h_sld,'ydata1',ydata)
setappdata(h_sld,'ydata2',smooth_ydata)
setappdata(h_sld,'fig_handle',h_fig);
setappdata(h_sld,'axes_handle',h_ax1);
Framelengthtext = uicontrol('Style','text','String','Smoothing Framelength:','unit','norm','Position',[0 0.01 0.5 0.05]);
setappdata(h_sld,'valText', Framelengthtext);
set(h_fig, 'WindowButtonDownFcn', @my_downbutton_function);
% Define the persistent variables to store the values of a and b
persistent pTgOnset
persistent pTgMid
if isempty(pTgOnset) || isempty(pTgMid)
pTgOnset = 0;
pTgMid = 0;
end
% Wait for the figure to be closed
waitfor(h_fig);
% Return the values of a and b using the nested function
[pTgOnset, pTgMid] = getOutput();
TgOnset = pTgOnset;
TgMid = pTgMid;
function my_downbutton_function(h_fig,~)
% This is the callback function that will be called when the mouse button is pressed down on the figure
% src: the source object that triggered the event (figure or axes handle)
% event: the event data structure
% Set a flag to indicate whether the mouse pointer is within the x range
hVerticalLines = getappdata(h_fig,'VerticalLines');
xVLC = get(hVerticalLines,'XData');
xVertLineCoord1 = xVLC{1}(1);
xVertLineCoord2 = xVLC{2}(1);
x = getappdata(h_fig,'xdata');
xrange = range(x);
xpercentage = 0.00625;
x_min1 = xVertLineCoord1-xrange*xpercentage;
x_max1 = xVertLineCoord1+xrange*xpercentage;
x_min2 = xVertLineCoord2-xrange*xpercentage;
x_max2 = xVertLineCoord2+xrange*xpercentage;
% Get the initial mouse press coordinates
initial_coords = get(gca, 'CurrentPoint');
initial_x = initial_coords(1,1);
[~,I] = min(abs([xVertLineCoord1 xVertLineCoord2]-initial_x));
if (initial_x >= x_min1 && initial_x <= x_max1) || (initial_x >= x_min2 && initial_x <= x_max2)
in_x_range = true;
set(h_fig, 'WindowButtonMotionFcn', @motion_callback_function);
set(h_fig, 'WindowButtonUpFcn', @up_callback_function);
else
in_x_range = false;
end
function motion_callback_function(h_fig,~)
% This is the callback function that will be called repeatedly while the mouse button is pressed down and the mouse is moving
% src: the source object that triggered the event (figure or axes handle)
% event: the event data structure
% Get the current mouse coordinates
current_coords = get(gca, 'CurrentPoint');
% Check if the mouse is within the y range or has moved out of it
if (initial_x >= x_min1 && initial_x <= x_max1) || (initial_x >= x_min2 && initial_x <= x_max2)
in_x_range = false;
end
hLineToDrag = hVerticalLines(I);
set(hLineToDrag,'XData',[current_coords(1, 1) current_coords(1, 1)]);
updatePlot2(h_fig)
end
function up_callback_function(~,~)
% This is the callback function that will be called when the mouse button is released
% src: the source object that triggered the event (figure or axes handle)
% event: the event data structure
% Reset the 'WindowButtonMotionFcn' and 'WindowButtonUpFcn' callbacks
set(gcf, 'WindowButtonMotionFcn', '');
set(gcf, 'WindowButtonUpFcn', '');
% Reset the in_y_range flag
in_x_range = false;
end
end
function updatePlot(h_fig,h_ax1)
x = getappdata(h_fig,'xdata');
y1 = getappdata(h_fig,'ydata1');
y2 = getappdata(h_fig,'ydata2');
xl = getappdata(h_fig,'xlims');
yl = getappdata(h_fig,'ylims');
h1 = plot(h_ax1,x,y1,'b');
setappdata(h_fig,'NoisyLine',h1)
hold on
stack = dbstack();
if numel(stack) > 1 && strcmp(stack(2).name,'TGLine')
h2 = plot(h_ax1,x,y2,'r');
setappdata(h_fig,'SmoothLine',h2)
else
if numel(stack) > 1 && strcmp(stack(2).name,'TGLine/slider_callback')
h2 = getappdata(h_fig,'SmoothLine');
delete(h2);
h2 = plot(x,y2,'r');
setappdata(h_fig,'SmoothLine',h2)
end
end
legend([h1, h2],'Original signal','Smoothed signal');
xlabel('Temperature / °C');
ylabel('DSC Signal / mW mg^{-1}');
xlim(xl);
ylim(yl);
end
function initiateVertLines(h_fig,h_ax1)
stack = dbstack();
if numel(stack) > 1 && strcmp(stack(2).name,'TGLine')
x = getappdata(h_fig,'xdata');
randt = randi([1,round(length(x)/2)-1]);
hVerticalLines = line([x(randt),x(randt)],get(h_ax1,'Ylim'),'Color','green');
randt = randi([round(length(x)/2)+1,length(x)]);
hVerticalLines = [hVerticalLines line([x(randt),x(randt)],get(h_ax1,'Ylim'),'Color','red')];
setappdata(h_fig,'VerticalLines',hVerticalLines)
end
end
function slider_callback(h_sld,~)
y1 = getappdata(h_sld,'ydata1');
h_fig = getappdata(h_sld,'fig_handle');
h_ax1 = getappdata(h_
基于Matlab模拟交互式玻璃化转变分析仪 (DSC).zip
版权申诉
32 浏览量
2023-03-03
06:55:13
上传
评论
收藏 37KB ZIP 举报
天天Matlab科研工作室
- 粉丝: 3w+
- 资源: 7258
最新资源
- GDAL-3.4.3-cp310-cp310-win-amd64.whl
- 基于matlab实现鸡群算法,群体智能算法的一种,立足鸡群的集合特征建立优化过程,与蜂群算法、蛙群算法有相似之处.rar
- 基于matlab实现很好的一个蜂群算法 基于matlab的源程序 从作者那要过来的.rar
- 基于matlab实现多目标优化NSGA3代码.rar
- 基于matlab实现多目标遗传算法(NSGA-III)matlab源代码.rar
- 基于matlab实现多尺度小波分析,用于分析时间多尺度周期
- vscode配置c/c 环境教程
- vscode配置c/c 环境教程
- 基于matlab实现电磁优化计算功能,进行线型规划优化电磁设计.rar
- 基于matlab实现带精英策略的非支配排序遗传算法matlab 源码.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈