In this paper a shunt active power filter (APF) has been used to eliminate current harmonics and compensate reactive current drawn by both nonlinear and linear loads. An online control method based on the discrete nonlinear model of inverters is proposed to APF. The optimal feedback controls the APF current in a very fast and efficient manner. Moreover, a novel extraction system based on the ynchronous reference frame is adopted as part of the control system of a single phase APF to provide the required reference signals for harmonic filtering and reactive power compensation. The compensation principle of the proposed algorithm is presented in detail. The circuit is simulated using PSCAD/EMTDC software and simulation results are presented to validate the effectiveness of the novel control scheme and proposed APF.