The frequency domain and the periodogram
Today let's understand the frequency domain and the periodogram, using ten weeks of a real restaurant's lunch covers as the running example.
Northside Diner is a lunch restaurant. Every day for ten weeks, seventy days in all, it logged how many covers it served, covers being the restaurant term for parties served a meal. Here is the whole seventy days, in the order they happened.
The line rises and falls, and it does that roughly once a week. That rise and fall is what this lesson is about: not just noticing that a series repeats, but naming the frequency it repeats at, and finding that frequency directly from the data with a tool called a periodogram.
Meet Northside Diner's ten weeks of lunch covers
Before anything else, build that series and look at its numbers directly, since every step from here on works with this same data.
The covers range from 68 on the slowest day to 178 on the busiest, averaging 119.2 a day with a standard deviation of 25.4. That's a lot of day-to-day swing for a restaurant that plans its staff and its prep roughly the same way every week.
Group those seventy days by weekday and average each group, and the swing turns out not to be random at all.
Wednesday is the slowest day, 97.1 covers on average. Friday is the busiest by far, 162.1, with Saturday close behind at 137.7. That's a real, repeating weekly pattern, the same shape week after week, not just a few Fridays that happened to be busy.
Saying "there's a weekly pattern" is a start, but it isn't precise. The next step gives that pattern a name that can actually be measured.
A repeating wave has a frequency, a period, and an amplitude
A repeating pattern like Northside Diner's weekly rise and fall can be written as a wave: a curve that goes up, comes back down, and repeats the same shape over and over. Three numbers describe any such wave completely.
The period is how many days pass before the wave repeats itself exactly. The frequency is how many times it repeats in one day, which is just 1 divided by the period. And the amplitude is how far the wave swings above, and below, its centre, not the level it sits at.
Pull the 7-day wave out of Northside Diner's series on its own, with no noise and no second wave mixed in, and those three numbers become concrete.
This wave takes 7 days to repeat, so its period is 7 days. Its frequency is 1/7, about 0.142857 cycles a day. And its amplitude is 30: the red point marks day 5, where the wave sits exactly 30 covers above its centre, and by the same symmetry it swings 30 covers below centre exactly half a cycle later, the dashed lines marking that ceiling and floor.
This one wave, on its own, is already most of the shape behind Northside Diner's Friday peak. But it isn't the whole shape. The next step adds a second wave on top of it.
Building the real shape by summing two waves
Northside Diner's real Friday peak is sharper than one 7-day wave alone can produce, and its early-week dip is flatter. That's because a second, smaller wave rides on top of the first one, a wave that repeats every 3.5 days instead of every 7.
Toggle between line and point below to see all three curves that build Northside Diner's real shape: the 7-day wave, the 3.5-day wave, and their sum.
The 3.5-day wave has amplitude 12, a little under half the 7-day wave's 30, and it completes two full cycles inside every seven days, twice the 7-day wave's frequency. That makes it what's called a harmonic of the 7-day wave. Added together, the two waves peak on the same day, Friday: 30 from the 7-day wave plus 12 from the 3.5-day wave, 42 in total, a sharper peak than the 7-day wave gives on its own.
This is the whole idea behind splitting a series into waves: any repeating shape, however complicated, can be built by adding together enough sine and cosine waves at the right frequencies and amplitudes. Northside Diner's real series needs only two.
Quick check: frequency, period and amplitude
The widget above also carries the 3.5-day wave, amplitude 12, completing one full cycle every 3.5 days.
spec.pgram() turns a noisy series into a list of frequencies
Northside Diner's real series isn't two clean waves. It's those two waves plus random noise, day-to-day variation with no pattern at all. Picking frequencies out of a series like that by eye, the way the last two steps did, doesn't work once noise is mixed in. What's needed is a tool that checks every candidate frequency at once and reports how much of the series' variation each one explains.
That tool is called a periodogram, and base R builds one with a function called spec.pgram(). Feed it Northside Diner's actual covers, noise included, and see what it finds.
spec.pgram() checked 35 candidate frequencies, one for every k/70 from 1/70 up to 0.5. A series sampled once a day can never show a cycle faster than once every two days, which is why the frequencies stop at 0.5. The arguments above turn off every adjustment spec.pgram() knows how to make: taper = 0 and detrend = FALSE skip its usual tapering and trend removal, demean = TRUE only centres the series, and fast = FALSE keeps the frequency grid exactly at k/70. What comes back is a raw periodogram, untouched by any smoothing. For each frequency it returns a number called an ordinate: how strongly the series moves in step with a wave of exactly that frequency. The taller the ordinate, the more of the series' variation that frequency explains.
Two ordinates tower over the rest. Frequency 0.142857, which is 1/7, the exact frequency of the 7-day wave built into this series, has ordinate 14,997.4. Frequency 0.285714, 2/7, the exact frequency of the 3.5-day wave, has ordinate 3,576.7, a good deal smaller but still far ahead of anything else. Every other ordinate in the table sits under 350.
That's the periodogram working as intended: both real frequencies, 1/7 and 2/7, come straight out of a noisy series, with no need to know in advance where to look.
Away from the two real peaks, the periodogram is jagged
Those two tall ordinates are only 2 of the 35 the periodogram computed. Read the other 33 on their own, away from frequencies 1/7 and 2/7, and a different picture shows up: no smooth curve, no gentle slope down from the peaks, just a scatter of small numbers jumping around with no obvious pattern.
The two real peaks are left out of the chart below on purpose, since they already had their moment in the last step: the taller of the two alone stands over forty times higher than the tallest point on this floor, so on the same chart it would flatten the floor down to a line hugging zero. Toggle between line and point to see the floor's raggedness both ways.
That's genuinely what a raw periodogram looks like away from a real cycle: no smooth decline, no story, just noise. And this isn't a sign that anything went wrong. Northside Diner's series was built from exactly two waves plus noise, nothing else, and the periodogram still came back this jagged everywhere except at those two frequencies. The next step explains why that jaggedness never goes away, no matter how much data you add.
Why a raw ordinate does not get quieter with more data
A natural guess: an ordinate looks noisy with 70 days of data, so collecting more data, say 700 days instead of 70, should calm it down, the way a sample mean gets more precise with a bigger sample. Test that guess directly. Rebuild Northside Diner's series 400 times over, with fresh random noise each time, at 70 days and then again at 700 days, and watch what happens to one ordinate with no real cycle behind it and one that has a real cycle behind it.
Look at the floor ordinate first, the one near frequency 0.30 where nothing real repeats. At 70 days, across the 400 reruns, it averages 99.0 with a standard deviation of 95.0, almost as large as the mean itself, about 96%. At 700 days, ten times the data, it averages 94.7 with a standard deviation of 91.6, still about 97% of the mean. Ten times the data bought this ordinate nothing: it's exactly as unreliable at 700 days as it was at 70.
Now look at the real peak, the ordinate at frequency 1/7. At 70 days it averages 15,792.8 with a standard deviation of 1,821.0, about 11.5% of the mean. At 700 days it averages 157,602.8 with a standard deviation of 5,622.0, about 3.6% of the mean. This one did settle down. More data made it far more reliable.
So the same fix, more data, works for a real cycle and does nothing for noise. That's because of how spec.pgram() works: more days don't hand any single ordinate more information about itself, they just add more ordinates to the list, one for every new k/n. A real cycle's ordinate keeps concentrating the same total power onto fewer and fewer neighbouring frequencies as n grows, and that concentration is what settles it down. A floor ordinate has no such concentration to fall back on. It's just noise, computed the same jittery way no matter how long the series runs.
Closing quiz: reading and trusting a periodogram
A different series' periodogram comes back with one tall ordinate at frequency 0.25, and every other ordinate low and jagged, the same way Northside Diner's floor looked two steps back.
Your turn: read a period off a periodogram peak
pg, from a few steps back, still holds all 35 frequencies and their ordinates, in pg$freq and pg$spec. Find the biggest ordinate with which.max(), read off its frequency, then turn that frequency into a period.
Show answer
# Find the frequency of the biggest ordinate, then its period
which.max(pg$spec)
pg$freq[which.max(pg$spec)]
1 / pg$freq[which.max(pg$spec)]
#> [1] 10
#> [1] 0.1428571
#> [1] 7References
- Shumway, R.H. & Stoffer, D.S. (2017), Time Series Analysis and Its Applications (4th ed.) - Springer, Ch. 4 "Spectral Analysis and Filtering": the sum-of-waves representation, the periodogram, and its large-sample noise.
- Chatfield, C. (2003), The Analysis of Time Series: An Introduction (6th ed.) - Chapman & Hall/CRC, Ch. 7 "Spectral Analysis": frequency, period and the raw periodogram.
- Venables, W.N. & Ripley, B.D. (2002), Modern Applied Statistics with S (4th ed.) - Springer, Ch. 14 "Time Series Analysis": background for R's spec.pgram().
- R documentation, stats::spec.pgram()
Quick recap
You can now look at a repeating series and name what's really driving the repetition, not just eyeball it. To summarize:
- Any repeating series can be written as a sum of waves, each with a frequency (cycles per day), a period (days per cycle) and an amplitude (the swing above and below centre).
- spec.pgram() turns a series into one ordinate per candidate frequency, and the tallest ordinates point straight at the real cycles: 1/7 and 2/7 here.
- Away from a real cycle, an ordinate's relative noise does not shrink with more data. It stayed at about 96% to 97% of the mean whether the series ran 70 days or 700.
Next, you'll smooth the periodogram on purpose, trading away some of that noise for a steadier picture of the spectrum.