This paper describes in detail a flexible approach to nonstationary time series analysis based on a Dynamic Harmonic Regression (DHR) model of the Unobserved Components (UC) type, formulated within a stochastic state space setting. The model is particularly useful for adaptive seasonal adjustment, signal extraction and interpolation over gaps, as well as forecasting or backcasting. The Kalman Filter and Fixed Interval Smoothing algorithms are exploited for estimating the various components, with the Noise Variance Ratio and other hyperparameters in the stochastic state space model estimated by a novel optimization method in the frequency domain. Unlike other approaches of this general type, which normally exploit Maximum Likelihood methods, this optimization procedure is based on a cost function defined in terms of the difference between the logarithmic pseudo-spectrum of the DHR model and the logarithmic autoregressive spectrum of the time series. The cost function not only seems to yield improved convergence characteristics when compared with the alternative ML cost function, but it also has much reduced numerical requirements.