Algorithm to smooth a curve while keeping the area under it constant
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
If your curve is a sampled signal, "keeping the area constant" usually means preserving the integral, or in discrete terms, preserving the sum after accounting for sample spacing. That is possible during smoothing, but only if the smoothing kernel is normalized and the boundary handling is chosen carefully.
The simplest practical algorithm is: smooth with a normalized kernel, compute the original and smoothed areas, and rescale the smoothed curve by their ratio if boundary effects changed the total area. That gives you a visually smoother curve without changing the total mass.
Why Smoothing Changes Area
In theory, convolution with a normalized kernel preserves total area on an infinite domain. In practice, sampled finite curves have edges, and most smoothing implementations make some boundary assumption:
- zero padding
- reflection
- edge repetition
- truncated windows
Those edge rules can change the total sum slightly, especially near the ends.
A Practical Algorithm
Use this sequence:
- choose a smoothing kernel whose coefficients sum to
1 - smooth the discrete curve
- compute original area and smoothed area
- multiply the smoothed curve by
original_area / smoothed_area
That final normalization step fixes any drift introduced by boundaries.
A Runnable Python Example
The output areas should match closely, showing that smoothing and area preservation can be combined cleanly.
Why the Kernel Must Be Normalized
If the smoothing weights do not sum to 1, you are not merely smoothing. You are also scaling the signal. A moving average kernel or Gaussian-like kernel should therefore be normalized before use.
For example:
- good kernel:
[1, 2, 1] / 4 - bad kernel:
[1, 2, 1]
The unnormalized version changes both smoothness and area.
Boundary Handling Choices
Even with a normalized kernel, boundaries matter. Zero padding often suppresses the ends and reduces area. Reflection or edge replication usually preserves the shape more naturally.
If exact conservation matters, the safest rule is:
- use a reasonable boundary mode
- compute the total afterward
- rescale once
That is more reliable than assuming a specific boundary rule is always perfect.
When Rescaling Is Not Enough
If the curve must stay nonnegative, monotone in parts, or satisfy physical constraints, a simple smoothing-plus-rescaling step may not be enough. In those cases, use constrained optimization or spline fitting with integral constraints.
But for many signal-processing and data-cleaning tasks, normalized convolution plus final area correction is the right engineering tradeoff.
Common Pitfalls
- Using a smoothing kernel whose coefficients do not sum to
1. - Forgetting that finite-length boundary handling can change total area.
- Rescaling before smoothing instead of after smoothing.
- Preserving the total sum while accidentally introducing negative values or other unwanted artifacts.
- Confusing "preserve area" with "preserve every local feature." Smoothing still redistributes mass locally.
Summary
- Area-preserving smoothing is usually done with a normalized kernel plus a final rescaling step.
- Kernel normalization preserves total mass in principle; edge handling can still introduce drift.
- Compute the original and smoothed areas explicitly and correct by their ratio.
- Reflection or edge-aware boundaries usually behave better than zero padding.
- If stronger shape constraints matter, move from simple filtering to constrained fitting methods.
Related reading
- algorithm used to calculate 5 star ratings
- Algorithmic / coding help for a PySpark markov model
- Algorithmic issue determining user sessions
- Aligning rotated xticklabels with their respective xticks
- Algorithm to solve the points of a evenly-distributed / even-gaps spiral?
- Algorithm to sort pairs of numbers
- All intermediate steps should be transformers and implement fit and transform
- All runs are not visible on TensorBoard

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.