A magnetic field generator includes a substrate, a main generator coil, at least one field sensor, at least one shim coil, a driver circuit and a correction circuit. The main generator coil, the field sensor, and the shim coil are all disposed on the substrate. The driver circuit is coupled to drive the main generator coil with a driving current at a selected frequency. The correction circuit is coupled to receive a signal at the selected frequency from the at least one field sensor and, in response to deviations in the signal from a predefined baseline, to drive the at least one shim coil with a driving current having an amplitude configured to return the signal to the baseline.