Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. function [ t, gxyz, vxyz, dxyz] = process( filename, sampleRate ,xoffset,yoffset,zoffset )
  2. %Function takes in file name, desired sample rate
  3. %offsets are for axis parrallel to gravity (offset '-1' if so)
  4. %returns time array in span of sample rate
  5. %and arrays of data accn vel and disp data
  6.  
  7. %Reads in data from file
  8. rawData = csvread(filename,5,0);
  9.  
  10. %Raw data from file split into columns
  11. %and adjusted
  12. tr = rawData(:,1);
  13. gxr = ((rawData(:,2))-xoffset)*9.81;
  14. gyr = ((rawData(:,3))-yoffset)*9.81;
  15. gzr = ((rawData(:,4))-zoffset)*9.81;
  16.  
  17.  
  18. %arrays to be returned
  19. t = []
  20. %arrays of data to be combined
  21. gx = []
  22. gy = []
  23. gz = []
  24. vx = []
  25. vy = []
  26. vz = []
  27. dx = []
  28. dy = []
  29. dz = []
  30.  
  31.  
  32.  
  33. %smoothing values over sample rate
  34. tmax = round(max(tr),1) %duration of data
  35. sizeT = size(tr) %size of time array
  36. dataPoints = tmax/sampleRate %determine number of dataPoints for given sample rate
  37. span = round(sizeT(1)/dataPoints)%determine how many raw data points to average over
  38.  
  39.  
  40. %interates var i, up to length of data
  41. %in steps of 'span' previously calculated
  42. for i = 1:span:size(tr)-span
  43. t = [t tr(i)]
  44.  
  45. %averaging values of acceleration over the span
  46. %then appending to array
  47. gxavg = mean(gxr(i:i+span))
  48. gx = [gx gxavg]
  49.  
  50. gyavg = mean(gyr(i:i+span))
  51. gy = [gy gyavg]
  52.  
  53. gzavg = mean(gzr(i:i+span))
  54. gz = [gz gzavg]
  55.  
  56.  
  57.  
  58. %ATTEMPT AT INTERGRATING
  59. %'integrating' with function 'trapz' over accerlation
  60. %calculating velocity and appending to array
  61. % vxt = cumtrapz(gxr(i:i+span))
  62. %
  63. % vx = [vx vxt]
  64. %
  65. % vyt = cumtrapz(gyr(i:i+span))
  66. %
  67. % vy = [vy vyt]
  68. %
  69. % vzt = cumtrapz(gzr(i:i+span))
  70. %
  71. % vz = [vz vzt]
  72.  
  73.  
  74. end
  75.  
  76.  
  77.  
  78. %SUVAT this bad boy for some velocity
  79. uxt = 0;
  80. uyt = 0;
  81. uzt = 0;
  82.  
  83.  
  84. for i = 1:length(t)
  85.  
  86. %calculating velocties for time t
  87. vxt = uxt + gx(i)*sampleRate
  88. vyt = uyt + gy(i)*sampleRate
  89. vzt = uzt + gz(i)*sampleRate
  90.  
  91. %calculating displacement for time t
  92. % dxt = sum(cumtrapz(vx))
  93. % dyt = sum(cumtrapz(vy))
  94. % dzt = sum(cumtrapz(vz))
  95.  
  96. % dxt = uxt*sampleRate + 0.5*gx(i)*sampleRate*sampleRate
  97. % dyt = uyt*sampleRate + 0.5*gy(i)*sampleRate*sampleRate
  98. % dzt = uzt*sampleRate + 0.5*gz(i)*sampleRate*sampleRate
  99.  
  100.  
  101.  
  102. %appending velocities arrays
  103. vx = [vx vxt]
  104. vy = [vy vyt]
  105. vz = [vz vzt]
  106.  
  107. %appending displacement to array
  108. % dx = [dx dxt]
  109. % dy = [dy dyt]
  110. % dz = [dz dzt]
  111.  
  112. %setting u as v for next calculation
  113. uxt = vxt
  114. uyt = vyt
  115. uzt = vzt
  116. end
  117. %
  118. % %combining data arrays to structure to be returned
  119.  
  120.  
  121. dx = acc2disp(gx,1/sampleRate)
  122. dy = acc2disp(gy,1/sampleRate)
  123. dz = acc2disp(gz,1/sampleRate)
  124.  
  125. gxyz = struct('gx',gx, 'gy',gy, 'gz',gz) %accn array
  126. vxyz = struct('vx',vx, 'vy',vy, 'vz',vz) %vel array
  127. dxyz = struct('dx',dx, 'dy',dy, 'dz',dz) %disp array
  128. %
  129. %
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement