A method for controlling a power generating unit , the power generating unit comprises a power source , a machine side converter , a line side converter , a DC link electrically connected to an output of the machine side converter and an input of the grid side converter, and filter inductors (L) and filter capacitors (C) arranged at the output of the power generating unit, the method comprises - obtaining capacitor voltage signals (Ucap) in a measurement frame by measuring capacitor voltages (Ucap) of one or more of the filter capacitors, - determining a virtual synchronous machine rotational speed (ωVSM) and/or a synchronous machine angle (θVSM), where the derivative of the synchronous machine rotational speed (ωVSM) is indicative of a deviation between a power reference (Pref) for a desired power output of the power generating unit and a grid power (Pgrid) supplied by the power generating unit to the output combined with a damping power (Pd), - providing a voltage magnitude reference (Vqref) for the desired reactive power (Qgrid) to be generated by the line side converter , characterized in - filtering the capacitor voltage signals (Ucap), or a transformed capacitor voltage signals (Udq, Uαβ), by transforming the capacitor voltage signals (Ucap) from the measurement frame into a target frame (dq, αβ), with a compensation filter (Gff) into filtered capacitor voltage signals (Udq_f, Uαβ_f), where the compensation filter (Gff) is designed to reduce a magnitude of an impedance peak and/or shift the impedance peak , where the impedance peak is present in an impedance characteristic of the output of the power generating unit, and - determining a filter compensated voltage reference (Vdq_f, Vαβ_f) by combining the voltage magnitude reference (Vqref), or a transformation thereof (Vdqref, Vαβref), with the filtered capacitor voltage signals (Udq_f, Uαβ_f). - obtaining capacitor voltage signals (Ucap) in a measurement frame by measuring capacitor voltages (Ucap) of one or more of the filter capacitors, - determining a virtual synchronous machine rotational speed (ωVSM) and/or a synchronous machine angle (θVSM), where the derivative of the synchronous machine rotational speed (ωVSM) is indicative of a deviation between a power reference (Pref) for a desired power output of the power generating unit and a grid power (Pgrid) supplied by the power generating unit to the output combined with a damping power (Pd), - providing a voltage magnitude reference (Vqref) for the desired reactive power (Qgrid) to be generated by the line side converter , characterized in - filtering the capacitor voltage signals (Ucap), or a transformed capacitor voltage signals (Udq, Uαβ), by transforming the capacitor voltage signals (Ucap) from the measurement frame into a target frame (dq, αβ), with a compensation filter (Gff) into filtered capacitor voltage signals (Udq_f, Uαβ_f), where the compensation filter (Gff) is designed to reduce a magnitude of an impedance peak and/or shift the impedance peak , where the impedance peak is present in an impedance characteristic of the output of the power generating unit, and - determining a filter compensated voltage reference (Vdq_f, Vαβ_f) by combining the voltage magnitude reference (Vqref), or a transformation thereof (Vdqref, Vαβref), with the filtered capacitor voltage signals (Udq_f, Uαβ_f). <EOS>