If your system (mass = m, spring stiffness = k) is vibrating so its displacement is given by y = a*sin(wt)
(where w is not necessarily its natural frequency) then its velocity is given by v = aw*cos(wt).
From these formulae it follows that when the object is at its equilibrium position (t = 0, ?/w, 2?/w, ...) the system's total energy is m*(aw)2/2 (since it is entirely kinetic). Similarly when the object is at one of its two extreme positions (t = ?/(2w), 3?/(2w), 5?/(2w), ...) the system's total energy is k*a2/2 (since it is entirely potential).
When the characteristics of the vibration (a and/or w) are changing with time, then the changes in the system's total energy are the result of energy losses due to damping and/or work done on the system by some external force (or work done by the system on an external force). To take things further you need to know the damping model and the external force as a function of time. You can then set up the appropriate differential equation: this might have an analytical solution, or might require a numerical solution. Standard textbooks on dynamics will present the derivation and solution for the case where the damping is "viscous" and the external force varies sinusoidally.
[Note that if we have no damping and no external force, the system's total energy will be constant. We can then equate the two energy formulae above and solve for the system's natural frequency, hopefully getting w = ?(k/m).]