Curve smoothing
Algorithm design
Area preservation
Numerical methods
Data analysis

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.

Practice ML system design

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:

  1. choose a smoothing kernel whose coefficients sum to 1
  2. smooth the discrete curve
  3. compute original area and smoothed area
  4. multiply the smoothed curve by original_area / smoothed_area

That final normalization step fixes any drift introduced by boundaries.

A Runnable Python Example

python
1import numpy as np
2
3x = np.linspace(0, 10, 101)
4y = np.sin(x) + 0.2 * np.random.default_rng(0).normal(size=len(x)) + 2.0
5
6kernel = np.array([1, 4, 6, 4, 1], dtype=float)
7kernel = kernel / kernel.sum()
8
9y_smooth = np.convolve(y, kernel, mode="same")
10
11dx = x[1] - x[0]
12original_area = np.sum(y) * dx
13smoothed_area = np.sum(y_smooth) * dx
14
15y_preserved = y_smooth * (original_area / smoothed_area)
16
17print(round(original_area, 6))
18print(round(np.sum(y_preserved) * dx, 6))

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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice ML system design