{"id":90,"date":"2009-11-11T11:08:44","date_gmt":"2009-11-11T09:08:44","guid":{"rendered":"http:\/\/www.lookingatnothing.com\/?p=90"},"modified":"2011-01-04T17:01:04","modified_gmt":"2011-01-04T08:01:04","slug":"weighted-binning","status":"publish","type":"post","link":"https:\/\/lookingatnothing.com\/index.php\/archives\/90","title":{"rendered":"Weighted binning"},"content":{"rendered":"<p>Hello,<\/p>\n<p>Once again, I&#8217;d like to present a binning routine. This time it&#8217;s a weighted binning routine, which instead of simply summing all intensity in one bin of a certain width, it splits the intensity to neighbouring bins based on their proximity to the bin centres:<\/p>\n<p><a rel=\"attachment wp-att-92\" href=\"http:\/\/www.lookingatnothing.com\/?attachment_id=92\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-92\" title=\"Weighted binning\" src=\"http:\/\/www.lookingatnothing.com\/wp-content\/uploads\/2009\/11\/binning_weighted.png\" alt=\"Weighted binning\" width=\"812\" height=\"368\" srcset=\"https:\/\/lookingatnothing.com\/wp-content\/uploads\/2009\/11\/binning_weighted.png 812w, https:\/\/lookingatnothing.com\/wp-content\/uploads\/2009\/11\/binning_weighted-300x135.png 300w\" sizes=\"auto, (max-width: 812px) 100vw, 812px\" \/><\/a><\/p>\n<p>As is explained in the graphics, this implies that there is a loss of intensity required in order to keep the first and last bin in line with the other bins. The advantage of this binning function, however, is that it should result in a smooth binned curve with no moire-like effects.<\/p>\n<p>Speedwise, it bins a 2048&#215;2048 image in about 7 seconds on a 2.4 GHz Intel core 2 duo processor (single core), which is just as quick as the equal intensity binning routine discussed in a previous post. As always, if you have any suggestions or improvements, please let me know so I can update the code.<\/p>\n<p>The code itself can be found here: <a href=\"http:\/\/www.lookingatnothing.com\/wp-content\/uploads\/2009\/11\/binning_weighted.m\">binning_weighted code<\/a><\/p>\n<p>It is also given below (behind the &#8220;more&#8221; button). Leave comments please!<\/p>\n<pre><!--more-->\r\n<span style=\"color: #1800ff;\">function<\/span> [qbin,Ibin]=binning_weighted(q,I,nbins)\r\n<span style=\"font-family: Courier, Monaco, 'Courier New', Courier, monospace; line-height: normal; color: #009d24;\">%this function implements binning of intensity into bins. The intensity is<\/span>\r\n<span style=\"font-family: Courier, Monaco, 'Courier New', Courier, monospace; line-height: normal; color: #009d24;\">%divided between the bin centers depending on their distance to the center.<\/span>\r\n<span style=\"font-family: Courier, Monaco, 'Courier New', Courier, monospace; line-height: normal; color: #009d24;\">%This may cause the first and last bin to be filled inequally with<\/span>\r\n<span style=\"font-family: Courier, Monaco, 'Courier New', Courier, monospace; line-height: normal; color: #009d24;\">%intensity, so be careful when using those. Some intensity has to be<\/span>\r\n<span style=\"font-family: Courier, Monaco, 'Courier New', Courier, monospace; line-height: normal; color: #009d24;\">%discarded to alleviate this issue.<\/span><\/pre>\n<pre><span style=\"font-family: Courier, Monaco, 'Courier New', Courier, monospace; line-height: normal; color: #009d24;\">%reshape q and I to x by 1 vector\r\n<\/span><span style=\"font-family: Courier, Monaco, 'Courier New', Courier, monospace; line-height: normal;\">q=reshape(q,size(q,1)*size(q,2),1);<\/span>\r\n<span style=\"font-family: Courier, Monaco, 'Courier New', Courier, monospace; line-height: normal;\">I=reshape(I,size(I,1)*size(I,2),1);<\/span>\r\n<span style=\"font-family: Courier, Monaco, 'Courier New', Courier, monospace; line-height: normal;\">qmin=min(q);<\/span>\r\n<span style=\"font-family: Courier, Monaco, 'Courier New', Courier, monospace; line-height: normal;\">qmax=max(q);<\/span>\r\n<span style=\"font-family: Courier, Monaco, 'Courier New', Courier, monospace; line-height: normal;\">stepsize=(qmax-qmin)\/nbins;<\/span>\r\n<span style=\"font-family: Courier, Monaco, 'Courier New', Courier, monospace; line-height: normal; color: #009d24;\">%bin center positions<\/span>\r\n<span style=\"font-family: Courier, Monaco, 'Courier New', Courier, monospace; line-height: normal;\">binc=qmin+0.5*stepsize:stepsize:qmax-0.5*stepsize;<\/span>\r\n<span style=\"font-family: Courier, Monaco, 'Courier New', Courier, monospace; line-height: normal; color: #009d24;\">%sort q,I to q<\/span>\r\n<span style=\"font-family: Courier, Monaco, 'Courier New', Courier, monospace; line-height: normal;\">[qsort,qsorti]=sort(q);<\/span>\r\n<span style=\"font-family: Courier, Monaco, 'Courier New', Courier, monospace; line-height: normal;\">Isort=I(qsorti);<\/span>\r\n<span style=\"font-family: Courier, Monaco, 'Courier New', Courier, monospace; line-height: normal; color: #009d24;\">%first implementation: using a loop function<\/span>\r\n<span style=\"font-family: Courier, Monaco, 'Courier New', Courier, monospace; line-height: normal;\">Ibin=zeros(size(binc));<\/span>\r\n<span style=\"font-family: Courier, Monaco, 'Courier New', Courier, monospace; line-height: normal;\"><span style=\"color: #1800ff;\">for<\/span> bini=1:length(binc)<\/span><\/pre>\n<pre><span style=\"font-family: Courier, Monaco, 'Courier New', Courier, monospace; line-height: normal; color: #009d24;\"><span style=\"color: #000000;\"> <\/span>%first optimisation: only calculate the values that actually matter;<\/span>\r\n<span style=\"font-family: Courier, Monaco, 'Courier New', Courier, monospace; line-height: normal; color: #009d24;\"><span style=\"color: #000000;\"> <\/span>%success, these two find's reduce the calculation time from 57 seconds<\/span>\r\n<span style=\"font-family: Courier, Monaco, 'Courier New', Courier, monospace; line-height: normal; color: #009d24;\"><span style=\"color: #000000;\"> <\/span>%to 7 seconds. The region before the loop takes 1 second, so further<\/span>\r\n<span style=\"font-family: Courier, Monaco, 'Courier New', Courier, monospace; line-height: normal; color: #009d24;\"><span style=\"color: #000000;\"> <\/span>%optimisations will have to occur within the loop.<\/span>\r\n qi(1)=find(qsort&gt;(binc(bini)-stepsize),1,<span style=\"color: #bb00f7;\">'first'<\/span>);<span style=\"font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px;\"> <\/span>\r\n\r\n qi(2)=find(qsort&lt;(binc(bini)+stepsize),1,<span style=\"color: #bb00f7;\">'last'<\/span>);\r\n\r\n<span style=\"color: #009d24;\"><span style=\"color: #000000;\"> <\/span>%compute distances<span style=\"color: #000000; font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px;\"> <\/span><\/span>\r\n\r\n qdist=abs(qsort(qi(1):qi(2))-binc(bini));<span style=\"font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px;\"> <\/span>\r\n\r\n<span style=\"color: #000000;\"> <\/span>%compute the weighting factors<span style=\"color: #000000; font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px;\"> <\/span>\r\n\r\n weightfact=(qdist&lt;stepsize).*(1-qdist.\/stepsize);<span style=\"font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px;\"> <\/span>\r\n\r\n<span style=\"color: #000000;\"> <\/span>%sum the intensities in one bin<span style=\"color: #000000; font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px;\"> <\/span>\r\n\r\n Ibin(bini)=sum(Isort(qi(1):qi(2)).*weightfact);\r\n<span style=\"font-family: Courier, Monaco, 'Courier New', Courier, monospace; line-height: normal; color: #1800ff;\">end<\/span>\r\n<span style=\"font-family: Courier, Monaco, 'Courier New', Courier, monospace; line-height: normal;\">qbin=binc;<\/span>\r\n<div><span style=\"font-family: Courier, 'Times New Roman', 'Bitstream Charter', Times, serif; font-size: small;\"><span style=\"line-height: normal;\">\r\n<\/span><\/span><\/div><\/pre>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Hello, Once again, I&#8217;d like to present a binning routine. This time it&#8217;s a weighted binning routine, which instead of simply summing all intensity in <a class=\"mh-excerpt-more\" href=\"https:\/\/lookingatnothing.com\/index.php\/archives\/90\" title=\"Weighted binning\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1,53],"tags":[31],"class_list":["post-90","post","type-post","status-publish","format-standard","hentry","category-uncategorized","category-software-2","tag-software"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1gZ2v-1s","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/lookingatnothing.com\/index.php\/wp-json\/wp\/v2\/posts\/90","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lookingatnothing.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lookingatnothing.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lookingatnothing.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lookingatnothing.com\/index.php\/wp-json\/wp\/v2\/comments?post=90"}],"version-history":[{"count":5,"href":"https:\/\/lookingatnothing.com\/index.php\/wp-json\/wp\/v2\/posts\/90\/revisions"}],"predecessor-version":[{"id":276,"href":"https:\/\/lookingatnothing.com\/index.php\/wp-json\/wp\/v2\/posts\/90\/revisions\/276"}],"wp:attachment":[{"href":"https:\/\/lookingatnothing.com\/index.php\/wp-json\/wp\/v2\/media?parent=90"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lookingatnothing.com\/index.php\/wp-json\/wp\/v2\/categories?post=90"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lookingatnothing.com\/index.php\/wp-json\/wp\/v2\/tags?post=90"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}