{"id":7735,"date":"2016-07-29T09:00:21","date_gmt":"2016-07-29T13:00:21","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=7735"},"modified":"2016-07-28T19:09:11","modified_gmt":"2016-07-28T23:09:11","slug":"smooth-your-data","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2016\/07\/29\/smooth-your-data\/","title":{"rendered":"Smooth your data"},"content":{"rendered":"<div class=\"content\"><p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/25634-robust-spline-smoothing-for-1-d-to-n-d-data\"><tt>smoothn<\/tt><\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2025341\">Damien Garcia<\/a>.<\/p><p>As the title of Damien's entry states, <tt>smoothn<\/tt> is a fast and easy smoothing function for n-dimensional data. It uses a generalized cross-validation method to estimate the smoothing parameter, which affects the quality of the output. In layman's terms, it automatically does things for you so that you just pass in the noisy data and out comes the smoothed data. Damian includes links to two of his papers for technical reference for those interested in the detailed theory.<\/p><p>From what I've seen, his code is very well written (<a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/check-code-for-errors-and-warnings.html\">Code Analyzer<\/a> is green!), with plenty of error-checking and good use of vectorized code. It has extensive help, and he includes many different types of examples. Here is one that I liked.<\/p><p><b>Noisy image with missing data<\/b><\/p><p>Filling in missing data in an image?! What?!<\/p><pre class=\"codeinput\">n = 256;\r\n\r\n<span class=\"comment\">% Original image<\/span>\r\ny0 = peaks(n);\r\n\r\n<span class=\"comment\">% Noisy image with missing data<\/span>\r\ny = y0 + randn(size(y0))*2;\r\nI = randperm(n^2);\r\ny(I(1:n^2*0.5)) = NaN;      <span class=\"comment\">% lose 1\/2 of data<\/span>\r\ny(40:90,140:190) = NaN;     <span class=\"comment\">% create a hole<\/span>\r\n\r\n<span class=\"comment\">% Smoothed image<\/span>\r\nz = smoothn(y);             <span class=\"comment\">% &lt;-- That's all!!<\/span>\r\n\r\n<span class=\"comment\">% View<\/span>\r\nsubplot(2,2,1:2)\r\nimshow(y,[])\r\ntitle(<span class=\"string\">'Noisy corrupt data'<\/span>)\r\nsubplot(2,2,3)\r\nimshow(z,[])\r\ntitle(<span class=\"string\">'Recovered data ...'<\/span>)\r\nsubplot(2,2,4)\r\nimshow(y0,[])\r\ntitle(<span class=\"string\">'... compared with the actual data'<\/span>)\r\ncolormap(parula)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_smoothn\/potw_smoothn_01.png\" alt=\"\"> <p><b>Comments<\/b><\/p><p>Give this a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=7735#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/25634-robust-spline-smoothing-for-1-d-to-n-d-data#comments\">comment<\/a> for Damien.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_b40acdf0f10d42638b6fb778fdf6a83a() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='b40acdf0f10d42638b6fb778fdf6a83a ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' b40acdf0f10d42638b6fb778fdf6a83a';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2016 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_b40acdf0f10d42638b6fb778fdf6a83a()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2016a<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2016a<br><\/p><\/div><!--\r\nb40acdf0f10d42638b6fb778fdf6a83a ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\r\n% Jiro>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/25634-robust-spline-smoothing-for-1-d-to-n-d-data |smoothn|> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2025341 Damien\r\n% Garcia>.\r\n%\r\n% As the title of Damien's entry states, |smoothn| is a fast and easy\r\n% smoothing function for n-dimensional data. It uses a generalized\r\n% cross-validation method to estimate the smoothing parameter, which\r\n% affects the quality of the output. In layman's terms, it automatically\r\n% does things for you so that you just pass in the noisy data and out comes\r\n% the smoothed data. Damian includes links to two of his papers for\r\n% technical reference for those interested in the detailed theory.\r\n%\r\n% From what I've seen, his code is very well written\r\n% (<https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/check-code-for-errors-and-warnings.html\r\n% Code Analyzer> is green!), with plenty of error-checking and good use of\r\n% vectorized code. It has extensive help, and he includes many different\r\n% types of examples. Here is one that I liked.\r\n%\r\n% *Noisy image with missing data*\r\n%\r\n% Filling in missing data in an image?! What?!\r\n\r\nn = 256;\r\n\r\n% Original image\r\ny0 = peaks(n);\r\n\r\n% Noisy image with missing data\r\ny = y0 + randn(size(y0))*2;\r\nI = randperm(n^2);\r\ny(I(1:n^2*0.5)) = NaN;      % lose 1\/2 of data\r\ny(40:90,140:190) = NaN;     % create a hole\r\n\r\n% Smoothed image\r\nz = smoothn(y);             % <REPLACE_WITH_DASH_DASH That's all!!\r\n\r\n% View\r\nsubplot(2,2,1:2)\r\nimshow(y,[])\r\ntitle('Noisy corrupt data')\r\nsubplot(2,2,3)\r\nimshow(z,[])\r\ntitle('Recovered data ...')\r\nsubplot(2,2,4)\r\nimshow(y0,[])\r\ntitle('... compared with the actual data')\r\ncolormap(parula)\r\n\r\n%%\r\n% *Comments*\r\n%\r\n% Give this a try and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=7735#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/25634-robust-spline-smoothing-for-1-d-to-n-d-data#comments\r\n% comment> for Damien.\r\n\r\n##### SOURCE END ##### b40acdf0f10d42638b6fb778fdf6a83a\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_smoothn\/potw_smoothn_01.png\" onError=\"this.style.display ='none';\" \/><\/div><p>Jiro's pick this week is smoothn by Damien Garcia.As the title of Damien's entry states, smoothn is a fast and easy smoothing function for n-dimensional data. It uses a generalized cross-validation... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2016\/07\/29\/smooth-your-data\/\">read more >><\/a><\/p>","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/7735"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=7735"}],"version-history":[{"count":7,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/7735\/revisions"}],"predecessor-version":[{"id":7747,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/7735\/revisions\/7747"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=7735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=7735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=7735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}