Novel algorithm for synthesis of fiber gratings