Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab noise cleaning

  1. May 16, 2008 #1
    I'm attempting to clean impulse noise from a wave file in matlab. the file is two channels, about 7 million points in each channel. What I wish to do is keep points, that are below my trigger levels, and delete points (in both channels simultaneously) that are above the trigger levels for either channel. Code is below

    for x = 1:1:lnth
    if abs(sig(i,1)) > level1 || abs(sig(i,2)) > level2
    sig(i,:) = [];

    The code works and does what I want it to. The problem is that its very, very slow. Is there a way I can vectorize the code to make it faster? Or does anyone have any suggestions for a smarter algorithim (I've had some ideas, but havn't bothered to code any of them)

  2. jcsd
  3. May 16, 2008 #2


    User Avatar
    Science Advisor
    Gold Member

    Try masking the indices instead. If you wanted to copy the massaged results into a new array, for instance, do this:

    newsig = sig(abs(sig(:,1)) <= level1 & abs(sig(:,2)) <= level2,:)

    Should be faster than your for loop.
  4. May 16, 2008 #3
    thank you! SOOO much faster now.
  5. May 16, 2008 #4
    marcusl, i sent you a msg, get back to me when you can, thanks ^_^
  6. May 16, 2008 #5
    I'll post it here if someone else could help instead

    How would I write vectorized code to have the signal value in both channels be set to zero if it exceeds the limits like before, instead of "removing" the values that were above the trigger values
  7. May 19, 2008 #6


    User Avatar
    Science Advisor
    Gold Member

    This will do it:

    sig(abs(sig(:,1)) > level1 | abs(sig(:,2)) > level2,:) = 0;
  8. May 20, 2008 #7
    thanks a lot, i think i'm starting to catch on.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?