finished code for pulse generation by Joey Smith