Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. xdata = (0:0.1:2*pi)';
  2. y0 = sin(xdata);
  3.  
  4. Add noise to the signal with nonconstant variance.
  5.  
  6. % Response-dependent Gaussian noise
  7. gnoise = y0.*randn(size(y0));
  8.  
  9. % Salt-and-pepper noise
  10. spnoise = zeros(size(y0));
  11. p = randperm(length(y0));
  12. sppoints = p(1:round(length(p)/5));
  13. spnoise(sppoints) = 5*sign(y0(sppoints));
  14.  
  15. ydata = y0 + gnoise + spnoise;
  16.  
  17. Fit the noisy data with a baseline sinusoidal model, and specify 3 output arguments to get fitting information including residuals.
  18.  
  19. f = fittype('a*sin(b*x)');
  20. [fit1,gof,fitinfo] = fit(xdata,ydata,f,'StartPoint',[1 1]);
  21.  
  22. Examine the information in the fitinfo structure.
  23.  
  24. fitinfo
  25.  
  26. fitinfo = struct with fields:
  27. numobs: 63
  28. numparam: 2
  29. residuals: [63x1 double]
  30. Jacobian: [63x2 double]
  31. exitflag: 3
  32. firstorderopt: 0.0883
  33. iterations: 5
  34. funcCount: 18
  35. cgiterations: 0
  36. algorithm: 'trust-region-reflective'
  37. stepsize: 4.1539e-04
  38. message: 'Success, but fitting stopped because change in residuals less than tolerance (TolFun).'
  39.  
  40. Get the residuals from the fitinfo structure.
  41.  
  42. residuals = fitinfo.residuals;
  43.  
  44. Identify "outliers" as points at an arbitrary distance greater than 1.5 standard deviations from the baseline model, and refit the data with the outliers excluded.
  45.  
  46. I = abs( residuals) > 1.5 * std( residuals );
  47. outliers = excludedata(xdata,ydata,'indices',I);
  48.  
  49. fit2 = fit(xdata,ydata,f,'StartPoint',[1 1],...
  50. 'Exclude',outliers);
  51.  
  52. Compare the effect of excluding the outliers with the effect of giving them lower bisquare weight in a robust fit.
  53.  
  54. fit3 = fit(xdata,ydata,f,'StartPoint',[1 1],'Robust','on');
  55.  
  56. Plot the data, the outliers, and the results of the fits. Specify an informative legend.
  57.  
  58. plot(fit1,'r-',xdata,ydata,'k.',outliers,'m*')
  59. hold on
  60. plot(fit2,'c--')
  61. plot(fit3,'b:')
  62. xlim([0 2*pi])
  63. legend( 'Data', 'Data excluded from second fit', 'Original fit',...
  64. 'Fit with points excluded', 'Robust fit' )
  65. hold off
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement