We report simulated infrared (IR) spectra of deuterated water and ice using Car--Parrinello molecular dynamics with maximally localized Wannier functions. Experimental features are accurately reproduced within the harmonic approximation. By decomposing the line shapes in terms of intramolecular and intermolecular dipole correlation functions, we find that short-range intermolecular dynamic charge fluctuations associated to hydrogen bonds are prominent over the entire spectral range. Our analysis reveals the origin of several spectral features and identifies network bending modes in the far IR range.