Due to high demand, expect some shipping delays at this time - orders may not ship for up to 2-3 business days.
0

Help with Calibrating AS7341
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Help with Calibrating AS7341

by AxisCat on Sat May 07, 2022 11:57 am

Hi All,

New member here. I have been working with the AS7341 11-channel spectral sensor. Let me start by saying university for me was 30 years ago and I barely squeaked out a C average in Calculus. Either I just didn't understand it or I drank too much that semester. That latter excuse sounds a bit better.

So I have been digging through the application data the manufacturer provided and a cool spreadsheet they made available. In particular I am trying to understand how they are reconstructing the spectral response based on the counts from the sensors. More to the point I am trying to reverse engineer how they go from this the typical filter response (upper image from below) To this the spectral correction matrix curves (lower image from below):

Curves.png
image
Curves.png (124.48 KiB) Viewed 81 times


This is where my math skills are letting me down. I believe the correction data was derived from the filter data using Least Squares & Matrix Multiplication. I am totally stuck on how to proceed. I know I am probably missing a lot here, but can anyone give me some hints to get started on solving this?

Thanks,
Axis

AxisCat
 
Posts: 4
Joined: Thu Mar 03, 2022 5:56 am

Re: Help with Calibrating AS7341

by adafruit_support_mike on Mon May 09, 2022 8:22 pm

What source document has those graphs? I can't find them in the datasheet or any of the related documentation on the AMS product page.

adafruit_support_mike
 
Posts: 64491
Joined: Thu Feb 11, 2010 2:51 pm

Re: Help with Calibrating AS7341

by AxisCat on Tue May 10, 2022 6:01 am

Good morning and thank you for the reply. I found it in the 'tools and resources' section on AMS website. I don't know the rules about posting links put here it is: https://ams.com/as7341#tab/tools

The first resource in the list is an Excel spreadsheet. I downloaded the last item XXX-Light Detection and Color + Spectral Measurement which is a zip file that they provide for their evaluation kit.

Inside that archive is a PDF named: AS7341_AN000633_2-00.pdf

This is the folder structure to easily find it:
AS7341_EvalSW_ALS_v1-26-0\Documents\application notes\Calibration\AS7341_AN000633_2-00.pdf

I appreciate you looking at this

AxisCat
 
Posts: 4
Joined: Thu Mar 03, 2022 5:56 am

Re: Help with Calibrating AS7341

by adafruit_support_mike on Tue May 10, 2022 8:48 pm

I've looked through that PDF, but don't see the lower set of curves anywhere. Could you post the PDF you have please?

adafruit_support_mike
 
Posts: 64491
Joined: Thu Feb 11, 2010 2:51 pm

Re: Help with Calibrating AS7341

by AxisCat on Wed May 11, 2022 9:04 am

Hi and thanks for sticking with me on this. The bottom set of curves is in the spreadsheet (same folder as the PDF)

AxisCat
 
Posts: 4
Joined: Thu Mar 03, 2022 5:56 am

Re: Help with Calibrating AS7341

by adafruit_support_mike on Sat May 14, 2022 12:09 am

There we go.. thank you.

It looks like the spreadsheet is doing spectrum reconstruction: creating a graph of the light frequencies lighting the sensor from the digital readings of each sensor element. The sensor readings are single digital values, so the general goal is to convert each value to a range of amplitudes at multiple frequencies.

From the shapes of the lower curves, it looks like the math is based on Fourier transforms. That will take some explanation...

Mechanically, Fourier transforms are based on two parts: multiplication of sine waves and integration.

Integration is easiest to break down in discrete form.. if you start with a smooth continuous function (like a sine wave), you can approximate it by making a bar graph: a series of equal-width rectangles that meet the curve at their upper-left corner. It's easy to multiply the height of each rectangle by its width to get the area, then you can add the rectangle areas together to get an approximation of the area under the curve. The difference between the approximation and the real area will be a series of roughly-triangular segments between the top of the rectangle and the curve.

If you make the rectangles narrower, the triangular segments get smaller, and the difference between the combined area of the rectangles and the area under the smooth curve becomes smaller. The ideal situation would be to make the width of each rectangle 0, but that leaves you trying to get a useful value multiplying zero by infinity.

The entire subject of integral calculus involves finding the conditions where you *can* get a useful value from 'zero times infinity' and calculating that kind of value. (The subject of differential calculus mostly involves getting useful values from 'zero divided by zero'). The logical jump from 'small but finite' to 'infinitely small' is interesting, but doesn't add anything useful to the current subject.

The integral of a sine wave is zero because the curve spends equal amounts of time-and-amplitude positive and negative. But -- and this is a key point of interest -- the square of a sine wave can never have negative values. Any value multiplied by itself, positive or negative, produces a positive result. Therefore the integral of a sine wave multiplied by itself has to be a positive, nonzero value.

That leads us to the general subject of multiplying sine waves by each other. The algebra of periodic functions is interesting in general, but the relationship between multiplication and addition is especially interesting:

sin( f1 x f2 ) = (sin( f1 + f2 ) + sin( f1 - f2 )) / 2

For a frequency multiplied by itself:

sin( f1 x f1 ) = ( sin( f1 + f1 ) + sin( f1 - f1 ) ) / 2 = sin( 2f1 ) / 2

but any product of different frequencies is the sum of two sine waves.

Addition and subtraction are preserved under integration, so:

Integral( f1 + f2 ) = Integral( f1 ) + Integral( f2 )

both of which will always be zero for the frequency components in the product of sines at two different frequencies.

With all of that background, we can finally get to the payoff: the integral of the product of any two frequencies will always be zero if the frequencies are different. If both frequencies are the same, the integral will be nonzero and proportional to the amplitudes of the two input signals. We generally choose the amplitude of the test frequency to be 2 so the integral equals the amplitude of that frequency component in the input signal.

From there, we move to the concept of 'transforms': operations that turn a function of one variable (call it 't') to a function of another variable (call it 's'). For time-based signals, like the instantaneous amplitude of light, we treat the 'x' in sin(x) as a function of time: sin( x(t) ). The Fourier transform is a function that multiplies a periodic input signal by every possible frequency, and the value of Fourier-transform-of-sin-t( s ) is the integral of sin( t ) x sin( s ).

The only nonzero values of a Fourier transform are proportional to the amplitude of frequency components that exist in the input signal.

Since the Fourier transform takes a time-based signal as input and produces a graph of frequencies as output, we say it maps functions from 'the time domain' to 'the frequency domain'.

All of the math associated with the Fourier transform is reversible, so if we have a graph of frequencies as input, we can use a reverse Fourier transform to turn it back to a time-based signal. The time-domain function and the frequency-domain function are different but equivalent ways to represent the same signal.

The relationship between time-domain and frequency-domain lets us do useful and interesting things, like creating filters. An ideal 1kHz filter allows every frequency below 1kHz to pass through unchanged, while blocking every frequency higher than 1kHz completely. In the frequency domain we can represent that as a rectangular pulse whose value is 1 from 0Hz to +/-1kHz (negative frequencies are an effect of periodic interaction where signals seem to move backward: https://www.youtube.com/watch?v=uENITui5_jU) and 0 for all frequencies beyond +/-1kHz.

The frequency-domain representation of running a signal through the perfect 1kHz filter is simply multiplying the frequency-domain input signal by the frequency-domain filter pulse. Then we can translate that back to the time domain with a reverse Fourier transform. The reverse-transformed equivalent of multiplication in the frequency domain is an operation called 'convolution' which is roughly demonstrated by this video:

https://www.youtube.com/watch?v=MNzBFgw ... RobScallon

Instead of taking the integral for each frequency value, you start playing a new copy of the input signal, multiplied by the amplitude of the filter function, at each instant. The combined sum of all the time-offset signals is the time-domain convolution of signals multiplied in the frequency domain.

If we just reverse-transform the rectangular pulse of a perfect filter, we get the time-domain function sin( x ) / x, sometimes called 'sinc', which looks like this:

sinc.png
sinc.png (170.32 KiB) Viewed 19 times


The lower set of graphs look like sinc() curves, centered around different frequencies. Without doing the actual transforms, I think they're frequency-domain equivalents of the pulse shapes in the upper set of graphs.

That makes sense because of another neat thing you can do by moving between the time and frequency domains: math is reversible, so if you can convolve an input signal with a filter function to get a filtered output signal, you can deconvolve the output signal with the filter function to reconstruct the original input.

That applies directly to what's going on in the spreadsheet: each color segment is a bandpass filter for a given range of frequencies. Taking a reading from the sensor is functionally equivalent to convolving the input spectrum by the filter function to get a discrete-valued reading. If we deconvolve that reading with the filter function, we can get an approximation of the whole input spectrum.

Naturally the reconstruction isn't perfect.. operations like 'rounding to the nearest integer' and aren't fully reversible, and ideally reversible ones like 'divide by a large number' don't reverse well on computers with finite-precision math.. but you can get reasonably close.

Since we have multiple color filters, the deconvolved approximations from all of them can be combined to make a reasonably accurate approximation of the original input light spectrum.

adafruit_support_mike
 
Posts: 64491
Joined: Thu Feb 11, 2010 2:51 pm

Re: Help with Calibrating AS7341

by AxisCat on Sat May 14, 2022 11:29 am

Hello Mike,

Thank you so much for the very thorough explanation you gave me. Needless to say it is going to take some time for me to go through it piece by piece and digest it. I hope I can reach out to you in the future if after I get deeper into this I still have questions.

Best regards,
Axis

AxisCat
 
Posts: 4
Joined: Thu Mar 03, 2022 5:56 am

Re: Help with Calibrating AS7341

by adafruit_support_mike on Sat May 14, 2022 9:23 pm

Yeah, it takes a while to assimilate Fourier transforms, not least because of the notational shortcut of using complex exponentials as a substitute for trigonometry.

As a very simple first example, multiplying the square root of -1 by itself over and over produces the following pattern: i, -1, -i, 1, i, -1, -i, 1, ... As with any repeating pattern, we can treat the values as points around a circle. There are better reasons though.

The sine and cosine functions can be broken down into polynomial functions:

sin( x ) = x - x^3/3! + x^5/5! - x^7/7! + ...
cos( x ) = 1 - x^2/2! + x^4/4! - x^6/6! + ...

And the function e^x can be broken down into the polynomial function:

e^x = 1 + x + x^2/2! + x^3/3! + x^4/4! + ...

If we add the sine and cosine functions together we get:

sin( x ) + cos( x ) = 1 + x - x^2/2! - x^3/3! + x^4/4! + x^5/5! - ...

which looks a lot like the exponential power function with a +, +, -, - pattern applied to the terms. The repeated multiplication of i by itself has exactly that pattern.

If we merge i into the exponential power series, we get:

1(i^0) + x(i^1) + (ix)^2/2! + (ix)^3/3! + (ix)^4/4! + (ix)^5/5! + (ix)^6/6! + ... = 1 + ix - x^2/2! - i x^3/3! + x^4/4! + i x^5/5! - x^6/6! - i x^7/7! + ...

and we have an additional 'i' on every term from the polynomial expansion of sin( x ). That leads directly to the equation:

e^ix = cos( x ) + i sin( x )

which is known as Euler's formula. Leonhard Euler lived in the 1700s and knew about the sine, cosine, and exponential power functions. He gets the credit for working in the factor of i (whose existence and utility were under debate at the time), and producing the completely unexpected result:

e^(i pi) = cos( pi ) + i sin( pi ) = -1 + (i * 0)

or e^(i pi) = -1

A major result of that relationship is that multiplication by a complex number produces the same numeric results as rotation around the origin in a plane where one axis is regular numbers and the other axis is multiples of i (aka: the complex plane). That relationship becomes even more apparent when we convert the cartesian values cos(x),sin(x) to polar coordinates r (radius) and theta (angle). That lets us convert any complex number of the form (a + ib) to r(cos(theta), i sin(theta)) and then re^i theta.

From there, we can use the law of exponents -- multiplication can be reduced to addition of exponents -- to get a system where rotating a point re^i theta by an angle w is simply re^i(theta + w)

The function e^x is convenient to work with in calculus, and when you combine that with the utility of being able to handle spatial rotation by simple algebra, complex numbers and polar-exponential notation became the dominant way of doing physics-based math. The problem for a beginner is that the notation seems to be weirdly opaque and nonsensical.

Your life will be easier if you study complex exponentials because they're the basic vocabulary in which all the work is done. It's also easy to get lost in the notation though. The conversation-level descriptions I've given will help you connect the alphabet soup of symbols to the ideas underneath.

adafruit_support_mike
 
Posts: 64491
Joined: Thu Feb 11, 2010 2:51 pm

Please be positive and constructive with your questions and comments.