diff --git a/miscmaths.cc b/miscmaths.cc
index 3988cbb386445ed86274cf41e1b4af485fd2d016..a02259bcfacc12bf881abb247bb4b7c099554e1e 100644
--- a/miscmaths.cc
+++ b/miscmaths.cc
@@ -1549,6 +1549,25 @@ void powerspectrum(const Matrix &Mat1, Matrix &Result, bool useLog)
 }
 
 
+void element_mod_n(Matrix& Mat,double n)
+{
+ //represent each element in modulo n (useful for wrapping phases (n=2*M_PI))
+
+  double tmp;
+  for( int j=1;j<=Mat.Ncols();j++){
+    for( int i=1;j<=Mat.Nrows();i++){
+
+      if( !( (Mat(i,j)>0) && (Mat(i,j)<n) ) ){ 
+	tmp = ( Mat(i,j) - MISCMATHS::round(Mat(i,j)/n)*n );
+	Mat(i,j)= tmp > 0 ? tmp : tmp + n;
+      }    
+      
+    }
+    
+  }
+  
+}
+
 int nextpow2(int n)
 {
   return (int)pow(2,ceil(log(float(n))/log(float(2))));
diff --git a/miscmaths.h b/miscmaths.h
index 5ea0705499028ef185611f087c859ef2954caa4c..382d2e63262eb83349e31736c803294eef20757e 100644
--- a/miscmaths.h
+++ b/miscmaths.h
@@ -196,7 +196,8 @@ namespace MISCMATHS {
   ReturnMatrix corrcoef(const Matrix& mat, const int norm = 0);
   void symm_orth(Matrix &Mat);
   void powerspectrum(const Matrix &Mat1, Matrix &Result, bool useLog);
-  
+  void element_mod_n(Matrix& Mat,double n); //represent each element in modulo n (useful for wrapping phases (n=2*M_PI))
+
   // ols
   // data is t x v
   // des is t x ev (design matrix)