My teacher has a demodulated signal and he removed the DC offset with this one line of code in MATLAB:
y_dc = y - mean(y)
Can someone please explain how this line of code removes the DC offset?
My teacher has a demodulated signal and he removed the DC offset with this one line of code in MATLAB:
y_dc = y - mean(y)
Can someone please explain how this line of code removes the DC offset?
The DC offset is the mean. Any non-DC (i.e. AC) components will have zero mean, while contributing to fluctuation around the mean, when the signal is long enough. (if the signal is very short, then a partial cycle of a sinusoidal component at some frequency may have a non-zero average)
If you have a finite-length signal (y in your teacher's example), then by definition the DC component to that signal is it's average value. In Matlab, that average value is mean(y).
The concept of DC comes from frequency analysis, where a signal is a linear combination of signals at various frequencies. That "linear" part means that if you know a signal's DC component, you can eliminate it just by subtraction.
That's where the y - mean(y) comes from.
Pretty much any DC removal technique boils down to implicitly or explicitly calculating the DC component, and subtracting it out. Whether you're using Matlab, real computer code, or a capacitor and a resistor, it's all doing the same thing.