Function :l representing a sinusodial peak at around 400nm - x * sin(l) type function.. Able to generate this function algorithimcally? The function l is discretised into 10nm bands...
Assistant
User
I am trying to approximate a reflectance curve for a defined wavelength of spectral color. Ultimately the goal is an RGB triplet (via XYZ as needed.) ...
Assistant
Certainly! To create a function ( l(\lambda) ) representing a sinusoidal peak centered around 400 nm, discretized into 10 nm bands, you can algorithmically generate the values accordingly.
Let's clarify the requirements:
Wavelength domain (\lambda) (in nm), discretized into 10 nm intervals, say from some start (\lambda_{start}) to some end (\lambda_{end}).
A sinusoidal peak centered roughly at 400 nm.
The function is similar to ( l(\lambda) = x \cdot \sin(\text{some function of } \lambda) ).
You want to generate this function algorithmically for these discretized wavelengths.
Step 1: Define the wavelength range
Example: from 300 nm to 500 nm, with 10 nm steps: 300, 310, 320, ..., 500 nm.
Step 2: Create a function that generates a sinusoidal peak centered at 400 nm
To have the "peak" at 400 nm:
The sinusoid argument should be zero (or peak) at 400 nm.
A function like ( \sin(k(\lambda - 400)) ) will have a zero crossing at 400 nm, but we want a peak at 400 nm.
Since (\sin(0) = 0), but (\cos(0) = 1), it might be easier to use cosine for the peak:
where (k) controls the "width" of the sinusoidal peak.
Step 3: Adjust the banding (discretization) into 10nm bands
We generate values of (l(\lambda)) at (\lambda = 300, 310, \ldots, 500)
Example algorithm (in Python-like pseudocode):
python
import numpy as np
def generate_sinusoidal_peak(lambda_start=300, lambda_end=500, step=10, peak_center=400, amplitude=1, width=0.1):
"""
Generate discretized sinusoidal peak values over wavelength range.
Parameters:
lambda_start (int): start wavelength in nm
lambda_end (int): end wavelength in nm
step (int): discretization step in nm
peak_center (float): wavelength at which peak occurs (nm)
amplitude (float): amplitude of the sinusoidal peak
width (float): controls the width of the peak
Returns:
wavelengths (list): list of wavelengths (nm)
values (list): list of function values l(λ) at discretized bands
"""
wavelengths = list(range(lambda_start, lambda_end + 1, step))
values = []
for lmbda in wavelengths:
# Using cosine for a peak at lmbda = peak_center
val = amplitude * np.cos(width * (lmbda - peak_center))
values.append(val)
return wavelengths, values
# Example usage:
wls, vals = generate_sinusoidal_peak()
for wl, v in zip(wls, vals):
print(f"{wl} nm: {v:.3f}")
Explanation of parameters:
amplitude: peak height.
width: determines how quickly the cosine falls from the peak. Larger width means more oscillations (narrow wider peaks). For a single peak in the range, keep it small.
step: discretization step (10 nm as per your request).
Customizing to your task:
If you want the function ( l(\lambda) = x \times \sin(\lambda) )-type, but with a peak at ~400 nm, you can shift and scale the input to sine:
Would you like me to generate and plot such a function, or generate values for certain wavelength ranges?
Got it! You want to approximate a reflectance curve centered around a specific wavelength (like 400 nm), then convert that reflectance spectrum into an RGB color. The process will be:
Generate a reflectance curve ( R(\lambda) ) discretized over the visible range.
Convert the reflectance curve to XYZ tristimulus values using the CIE color matching functions.
Convert the XYZ values to RGB using a defined color space (e.g., sRGB).
Step 1: Generate the reflectance curve
Reflectance curves for narrow-band spectral reflectors (monochromatic colors) are often approximated by a bell-shaped curve around the peak wavelength. A sinusoidal peak can be used as a rough approximation.
You can approximate reflectance over the visible spectrum ((~380,nm) to (~730,nm)) discretized in 10 nm steps, centered on wavelength (\lambda_0), e.g., 400 nm.
Example Reflectance Function
Using a raised cosine bump centered at (\lambda_0):
Where (w) (half-width) controls the spread of the reflectance curve, e.g., (w = 20, nm).
Step 2: Convert Reflectance Curve to XYZ
Calculate:
[
X = k \sum_\lambda R(\lambda) S(\lambda) \overline{x}(\lambda) \Delta\lambda
]
[
Y = k \sum_\lambda R(\lambda) S(\lambda) \overline{y}(\lambda) \Delta\lambda
]
[
Z = k \sum_\lambda R(\lambda) S(\lambda) \overline{z}(\lambda) \Delta\lambda
]
( S(\lambda) ) is the spectral power distribution (SPD) of the illuminant (e.g., D65).
( \overline{x}(\lambda), \overline{y}(\lambda), \overline{z}(\lambda) ) are the CIE 1931 2° standard observer color matching functions.
( k ) is a normalization constant to ensure ( Y=1.0 ) for a perfect reflecting diffuser under the illuminant.
( \Delta \lambda ) is the wavelength step (e.g., 10 nm).
Step 3: Convert XYZ to sRGB
Use standard matrices and gamma correction to convert XYZ to sRGB values in [0,1].
Full Python Example Illustrating This
python
import numpy as np
import matplotlib.pyplot as plt
# Load or define color matching functions (CMFs) and illuminant here.
# For the example, we hardcode typical wavelength range 380-730 nm
wavelengths = np.arange(380, 731, 10) # 10 nm steps
# Dummy data just for example:
# In practice, use real CIE 1931 CMF data and Illuminant D65 or similar.
# Here we use placeholder arrays for demonstration.
cie_x = np.array([0.0014, 0.0042, 0.0143, 0.0435, 0.1344, 0.2839, 0.3483, 0.3362, 0.2908, 0.1954,
0.0956, 0.032, 0.0049, 0.0093, 0.0633, 0.1655, 0.2904, 0.4334, 0.5945, 0.7621,
0.9163, 1.0263, 1.0622, 1.0026, 0.8544, 0.6424, 0.4479, 0.2835, 0.1649, 0.0874,
0.0468, 0.0227, 0.0114, 0.0058, 0.0029, 0.0014, 0.0007, 0.0003, 0.0002, 0.0001,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000])
cie_y = np.array([0.0000, 0.0001, 0.0004, 0.0012, 0.0040, 0.0116, 0.0230, 0.0380, 0.0600, 0.0910,
0.1390, 0.2080, 0.3230, 0.5030, 0.7100, 0.8620, 0.9540, 0.9950, 0.9950, 0.9520,
0.8700, 0.7570, 0.6310, 0.5030, 0.3810, 0.2650, 0.1750, 0.1070, 0.0610, 0.0320,
0.0170, 0.0082, 0.0041, 0.0021, 0.0010, 0.0005, 0.0002, 0.0001, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000])
cie_z = np.array([0.0065, 0.0201, 0.0679, 0.2074, 0.6456, 1.3856, 1.7471, 1.7721, 1.6692, 1.2876,
0.8130, 0.4652, 0.2720, 0.1582, 0.0782, 0.0422, 0.0203, 0.0087, 0.0039, 0.0021,
0.0017, 0.0011, 0.0008, 0.0003, 0.0002, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000])
# Simplified D65 illuminant SPD (normalized)
d65_spd = np.ones_like(wavelengths) # assume constant illuminant for simplicity
def reflectance_curve(lambdas, peak_center=400, width=20):
R = np.zeros_like(lambdas, dtype=float)
for i, l in enumerate(lambdas):
dist = abs(l - peak_center)
if dist <= width:
R[i] = 0.5 * (1 + np.cos(np.pi * dist / width))
else:
R[i] = 0.0
return R
def calculate_xyz(R, spd, cmf_x, cmf_y, cmf_z, delta_lambda=10):
k = 100 / np.sum(spd * cmf_y * delta_lambda) # normalization for Y=100 white
X = k * np.sum(R * spd * cmf_x * delta_lambda)
Y = k * np.sum(R * spd * cmf_y * delta_lambda)
Z = k * np.sum(R * spd * cmf_z * delta_lambda)
return X, Y, Z
def xyz_to_srgb(X, Y, Z):
# Matrix for CIE XYZ to linear sRGB (D65)
M = np.array([[ 3.2406, -1.5372, -0.4986],
[-0.9689, 1.8758, 0.0415],
[ 0.0557, -0.2040, 1.0570]])
rgb_linear = np.dot(M, np.array([X, Y, Z]) / 100)
# Apply gamma correction
def gamma_correct(c):
a = 0.055
return np.where(c <= 0.0031308, 12.92 * c, (1 + a) * (c ** (1/2.4)) - a)
rgb = gamma_correct(rgb_linear)
rgb = np.clip(rgb, 0, 1) # Clamp between 0 and 1
return rgb
# Generate reflectance centered at 400nm
R = reflectance_curve(wavelengths, peak_center=400, width=20)
X, Y, Z = calculate_xyz(R, d65_spd, cie_x, cie_y, cie_z)
rgb = xyz_to_srgb(X, Y, Z)
print(f"XYZ = ({X:.2f}, {Y:.2f}, {Z:.2f})")
print(f"RGB = {rgb}")
# Plot reflectance curve
plt.plot(wavelengths, R, label="Reflectance")
plt.xlabel("Wavelength (nm)")
plt.ylabel("Reflectance")
plt.title("Reflectance Curve centered at 400 nm")
plt.legend()
plt.show()
What to do next?
Replace the dummy CMF and illuminant SPD arrays with full accurate data from standard sources.
Tune the width parameter for your desired spectral sharpness.
To approximate other peak wavelengths, change peak_center.
Once you have the RGB values for different reflectance spectra, you can display or use them in your project.
If you want, I can help provide:
Accurate CMF and illuminant data.
Extended code for batch computing multiple wavelengths.
Explanation of the XYZ-RGB conversion and handling gamma correctly.