You've defined I2, Q2 and Period several times. Also don't define a series at a wrong place after an if(..). Setting a series to 6 or 50 will immediately crash. Also, check if the atan2 can be zero - in that case dividing by it would crash too.