Mazdas are very sensitive to ignition problems.
Tune it up. If it does not solve the problem, replace the air flow sensor located between the throttle body and the air filter under the hood driver side.
Don't forget to change the fuel filter when you tune it up.
Good luck.