Advertisement
Guest User

rebinning

a guest
May 28th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scilab 6.19 KB | None | 0 0
  1. clf
  2. clear
  3.  
  4. function [magField, magnetisation] = assignData(hysteresisType)
  5.     filename = uigetfile(["*.xls"],"C:\Users\oliverp\Desktop\Scilab Graphing Files", "Choose " + string(hysteresisType) + " file to be rebinned", %f)
  6.     data = readxls(filename)
  7.     datasheet = data(1)
  8.     magField = datasheet(2:$, 1)
  9.     magnetisation = datasheet(2:$, 2)
  10. endfunction
  11.  
  12. function [fieldSample1, magSample1, fieldSample2, magSample2, fieldSample3, magSample3] = splitData(xData, yData)
  13.     i = 1
  14.     j = 1
  15.     while( increasingField(xData, i) == %T ) then
  16.         if(xData == 0) then
  17.             fieldSample1(j) == %nan
  18.             magSample1(j) == %nan
  19.         end
  20.         fieldSample1(j) = xData(i)
  21.         magSample1(j) = yData(i)
  22.         i = i + 1
  23.         j = j + 1
  24.     end
  25.     //disp('size of m1Inc = ' + string(j))
  26.     j = 1
  27.     while( increasingField(xData, i) == %F ) then
  28.         if(xData == 0) then
  29.             fieldSample2(j) == %nan
  30.             magSample2(j) == %nan
  31.         end
  32.         fieldSample2(j) = xData(i)
  33.         magSample2(j) = yData(i)
  34.         i = i + 1
  35.         j = j + 1
  36.     end
  37.     //disp('size of m1Dec = ' + string(j))
  38.     j = 1
  39.     while( ( increasingField(xData, i) == %T ) & ( xData(i) ~= xData($) ) ) then
  40.         if(xData == 0) then
  41.             fieldSample3(j) == %nan
  42.             magSample3(j) == %nan
  43.         end
  44.  
  45.         fieldSample3(j) = xData(i)
  46.         magSample3(j) = yData(i)
  47.         i = i + 1
  48.         j = j + 1
  49.     end
  50.     //disp('size of m2Inc = ' + string(j))
  51.     disp('fields segregated')
  52. endfunction
  53.  
  54. function fldInc = increasingField(magneticField, i)
  55.     if ( ( magneticField(i) == magneticField($) ) | ( magneticField(i) == magneticField(1) ) ) then
  56.         fldInc = %T
  57.     /*elseif ( (magneticField(i-1) > magneticField(i) ) & ( magneticField(i) == magneticField(i+1) ) ) then
  58.         fldInc = %T
  59.     elseif ( (magneticField(i-1) < magneticField(i) ) & ( magneticField(i) == magneticField(i+1) ) ) then
  60.         fldInc = %F*/
  61.     elseif  ( magneticField(i+1) < magneticField(i)  ) then
  62.         fldInc = %F
  63.     elseif ( magneticField(i+1) > magneticField(i) ) then
  64.         fldInc = %T
  65.     end
  66. endfunction
  67.  
  68. function d = delta(xSpace, i) //finds spacing in array between the ith and i+1th component
  69.     if( xSpace(i) == xSpace($)) then
  70.         d = abs(xSpace(i - 1) - xSpace(i))
  71.     else
  72.         d = abs(xSpace(i + 1) - xSpace(i))
  73.     end
  74. endfunction
  75.  
  76. function k = closestDat2Bin( datField, rebinField, j ) // Finds closest datapoint to the rebin point
  77.     n = 1
  78.     diff = []
  79.     minVal = -1
  80.     minCol = -1
  81.     //disp("size of mField = " + string( size(datField, 1) ) )
  82.     for n = 1:size(datField, 1)  do
  83.         diff(n) = abs( datField(n) - rebinField(j) )
  84.     end
  85.     [minVal, minCol] = min(diff)
  86.     k = minCol
  87.     //disp(" k = " + string(k))
  88. endfunction
  89.  
  90. function i = lhsDat2Bin( datField, rebinField, j, k) //finds left-most datapoint that is still closer to rebin field j than j-1
  91.     if( j == 1) then
  92.         i = k
  93.     elseif (j ~= 1 ) then
  94.  
  95.         while( abs(magneticField(k-1) - x(j)) < abs(magneticField(k-1) - x(j-1)) )
  96.             k = k - 1
  97.         end
  98.         i = k
  99.         //disp(" i = " + string(i))
  100.     end
  101. endfunction
  102.  
  103. function yOut = rebinning(magneticField, magnetisation, x)
  104.     //yOut = zeros(size(x,2), 1)
  105.     yMag = zeros( size( x, 1 ), 1 )
  106.     for j = 1:(size(x, 1)-1) do
  107.         //disp('j = ' + string(j))
  108.         n = 0
  109.         i = 0
  110.  
  111.         k = closestDat2Bin( magneticField, x, j)
  112.        
  113.         if delta(magneticField, k) <= delta(x, 1) then //if measurement increment is smaller than rebinning increment
  114.             i =  lhsDat2Bin( magneticField, x, j, k)
  115.             while ( abs(magneticField(i) - x(j)) < abs(magneticField(i) - x(j+1)))  //while measurement field is closest to x(j) rebinned data point
  116.                 yMag(j) = yMag(j) + magnetisation(i)
  117.                 n = n + 1
  118.                 //disp(n)
  119.                 if( magneticField(i) ~= magneticField($) ) then
  120.                     i = i + 1
  121.                 else
  122.                     break
  123.                 end
  124.             end
  125.             if n ~= 0 then
  126.                 yMag(j) = yMag(j)/n
  127.             else
  128.                 yMag(j) = magnetisation(k)
  129.             end
  130.             //disp('yMag = ' + string(yMag(j)) )
  131.         else
  132.             if ( ~isnan(magnetisation(k)) ) then
  133.                yMag(j) = magnetisation(k)
  134.             end
  135.         end
  136.     end
  137.     j = j + 1
  138.     k = closestDat2Bin( magneticField, x, j)
  139.     yMag(j) = magnetisation(k)
  140.     yOut = yMag
  141. endfunction
  142.  
  143. // Assign arrays from excel files
  144. disp("Pick LCMO hysteresis file:")
  145. [xDataL, yDataL] = assignData("LCMO")
  146.  
  147. //disp('sheet assigned')
  148. disp("Pick YBCO hysteresis file:")
  149. [xDataY, yDataY] = assignData("YBCO")
  150.  
  151. disp("Pick YSL-1 hysteresis file:")
  152. [xDataYSL, yDataYSL] = assignData("YSL")
  153.  
  154. // Create a struct for each array to be rebinned
  155. LCMOhyst = struct('x1', ,'x2', , 'x3', ,'y1', ,'y2', ,'y3',  ,'sample', 'LCMO')
  156. [LCMOhyst.x1, LCMOhyst.y1, LCMOhyst.x2, LCMOhyst.y2, LCMOhyst.x3, LCMOhyst.y3] = splitData(xDataL, yDataL)
  157.  
  158. YBCOhyst = struct('x1', ,'x2', , 'x3', ,'y1', ,'y2', ,'y3',  ,'sample', 'YBCO')
  159. [YBCOhyst.x1, YBCOhyst.y1, YBCOhyst.x2, YBCOhyst.y2, YBCOhyst.x3, YBCOhyst.y3] = splitData(xDataY, yDataY)
  160.  
  161. // Rebinning section
  162. x1 = 0.03:0.01:0.99
  163. disp('Rebinning part 1')
  164. y1L = rebinning( LCMOhyst.x1, LCMOhyst.y1, x1' )
  165. y1Y = rebinning( YBCOhyst.x1, YBCOhyst.y1, x1' )
  166. disp('part 1 Completed')
  167.  
  168. x2 = 1:-0.01:-0.99
  169. disp('Rebinning part 2')
  170. y2L = rebinning( LCMOhyst.x2, LCMOhyst.y2, x2' )
  171. y2Y = rebinning( YBCOhyst.x2, YBCOhyst.y2, x2' )
  172. disp('part 2 Completed')
  173.  
  174. x3 = -1:0.01:0.1
  175. disp('Rebinning part 3')
  176. y3L = rebinning( LCMOhyst.x3, LCMOhyst.y3, x3' )
  177. y3Y = rebinning( YBCOhyst.x3, YBCOhyst.y3, x3' )
  178. disp('part 3 Completed')
  179.  
  180. //Concatenate sections of hysteresis into final rebinned data
  181. x = cat(1, x1', x2', x3')
  182. yLCMO = cat(1, y1L, y2L, y3L)
  183. yYBCO = cat(1, y1Y, y2Y, y3Y)
  184.  
  185. //Normalise the maximum values for manipulation
  186. yYBCO = yYBCO * max(yDataYSL)/max(yYBCO)
  187. yLCMO = yLCMO * max(yDataYSL)/max(yLCMO)
  188.  
  189. // Plot data
  190. plot(x, yLCMO, 'r')
  191. plot(x, yYBCO, 'b')
  192. plot(x, yYBCO + yLCMO, '-oblk')
  193. plot(xDataYSL, yDataYSL, 'g')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement