Simple Scaling Issue
j s s
Hi all,
In theory this is a simple task. I have a wave that I want to histogram. I need to account for the bin width for when I'm histogramming. Therefore I need to scale the wave by multiplying it by the bin width. My results are not looking great and I'm getting a crazy histogram that I'm not expecting. Hoping you folks can help catch what I'm assuming is an obvious answer.
function BinWidthAdjust(PulseInt, Range, BinNumber)
wave PulseInt
variable Range, BinNumber
PulseInt = (PulseInt*(Range/BinNumber)) //Multiplying wave by bin width
end
function PulseIntHist(PulseInt, Range, BinNumber, RunTime, trialnum)
wave PulseInt
variable Range, BinNumber, RunTime
String trialnum //Spectrum type meaning ToF or Energy Spectrum
variable i, N=numpnts(PulseInt), Index
Make/N=(BinNumber+1)/O PulseIntHistt
for (i=0; i<N; i+=1)
Index = (PulseInt[i]/(Range/BinNumber)) // divided by bin widths
if ((0 < Index) && (Index < BinNumber))
PulseIntHistt[Index] +=1
endif
endfor
PulseIntHistt = PulseIntHistt/RunTime
String EnergHist
EnergHist = "PulseInt"+trialnum+"_h"
Rename PulseIntHistt $EnergHist
end
wave PulseInt
variable Range, BinNumber
PulseInt = (PulseInt*(Range/BinNumber)) //Multiplying wave by bin width
end
function PulseIntHist(PulseInt, Range, BinNumber, RunTime, trialnum)
wave PulseInt
variable Range, BinNumber, RunTime
String trialnum //Spectrum type meaning ToF or Energy Spectrum
variable i, N=numpnts(PulseInt), Index
Make/N=(BinNumber+1)/O PulseIntHistt
for (i=0; i<N; i+=1)
Index = (PulseInt[i]/(Range/BinNumber)) // divided by bin widths
if ((0 < Index) && (Index < BinNumber))
PulseIntHistt[Index] +=1
endif
endfor
PulseIntHistt = PulseIntHistt/RunTime
String EnergHist
EnergHist = "PulseInt"+trialnum+"_h"
Rename PulseIntHistt $EnergHist
end
1) Your function BinWidthAdjust() isn't called, so it's hard to know how it's used. It's best to provide a minimal example that we can run. If it depends on a data set, an Igor experiment file would be good, with all procedure files adopted. If it can be expressed in code, provide a procedure that includes code to create waves that are needed as input. In either case, explain what you expect, and how the result differs.
2) It's conceivable that there's a better solution, maybe one using Igor's histogram command. Can you explain what you are trying to do? If you're on the wrong track fixing a bug in the wrong track won't help much :)
May 13, 2019 at 12:46 pm - Permalink
In reply to 1) Your function… by johnweeks
Apologies for the incomplete explanation
I have a wave, PulseInt, in units of energy. This data set was recorded by a detector. The detector spit out some nonsense ADC values. I calibrated the detector and separately have found the energy scaling by fitting a gaussian to a well known calibration source. After histogramming and scaling the wave, I get this energy spectrum.
I need to stretch the spectrum horizontally by a factor of the binwidth = Range/BinNumber so the peak is scaled more towards the right.
I thought multiplying the PulseInt wave by the binwidth would achieve this but this is not the result I get.
Here's a better snippet that integrates the BinWidthAdjust() function.
wave PulseInt
variable Range, BinNumber, RunTime
String trialnum //Spectrum type meaning ToF or Energy Spectrum
pulseInt = PulseInt*(Range/BinNumber) //Previously the separate BinWidthAdjust() function
variable i, N=numpnts(PulseInt), Index
Make/N=(BinNumber+1)/O PulseIntHistt
for (i=0; i<N; i+=1)
Index = (PulseInt[i]/(Range/BinNumber)) // divided by bin widths
if ((0 < Index) && (Index < BinNumber))
PulseIntHistt[Index] +=1
endif
endfor
PulseIntHistt = PulseIntHistt/RunTime
String EnergHist
EnergHist = "PulseInt"+trialnum+"_h"
Rename PulseIntHistt $EnergHist
end
May 13, 2019 at 01:51 pm - Permalink
In reply to 1) Your function… by johnweeks
I instead get this histogram when I multiply the PulseInt by the bindwidth
May 13, 2019 at 01:52 pm - Permalink
I still don't quite understand the inputs and desired outputs. Let's do the minimal runnable example- if you could post an Igor experiment file (are you allowed to share a data set?) with input data and the commands to run your code, perhaps we will understand a bit better. Is it the shape of the output that's wrong, or simply the X values on the horizontal axis? You posted two pictures- your second picture you said, "Instead I get this..." which implies that is the output you get and that something is wrong with it. The first picture is quite different- what is it?
May 14, 2019 at 09:11 am - Permalink