Fix Filtering Frequencies Code in Matlab w/o Built-In Filters

Click For Summary

Discussion Overview

The discussion revolves around a Matlab coding issue related to filtering frequency components from a .wav sound file. Participants are focused on correcting the provided code to achieve the desired filtering effect without using built-in filters, addressing both logical errors and performance concerns.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant identifies that the while loop may be causing an infinite loop due to its conditional structure, suggesting that the loop's condition does not change within the loop.
  • Another participant emphasizes the importance of checking for infinite loops and suggests adding display statements to monitor the loop's behavior.
  • Concerns are raised about the efficiency of using programmed loops in Matlab, which may lead to slow performance.
  • There is a suggestion that the participant should refer to individual elements of the vector s (i.e., s(i)) instead of the entire vector within the while loop, although this does not resolve the infinite loop issue.
  • A later reply expresses gratitude for the hints provided, indicating that the feedback was helpful.

Areas of Agreement / Disagreement

Participants generally agree on the identification of the infinite loop issue and the inefficiency of the current implementation, but there is no consensus on a definitive solution to the overall problem.

Contextual Notes

The discussion highlights limitations in the original code, including potential infinite loops and inefficiencies in Matlab's handling of loops, but does not resolve these issues completely.

roam
Messages
1,265
Reaction score
12
I want to write a Matlab code which removes all frequency components from a .wav sound file except those within ##±25 Hz## of ##523 Hz## as well as its harmonics (up to the Nyquist frequency). I want to do this without using any built in filters.

Here is my code so far:

Code:
[s, Fs] = wavread('chord.wav'); % Reading the .wav sound file
sNew = zeros(size(s)); % creating a matrix of zeros the size of s

for i = 1:length(sNew)
    for N=1:1:(Fs/(2*(523+25))); % number of harmonics up to Nyquist freq
        while abs(s-(523.*N))<25 % while within ±25 range of each of the harmonics
            sNew(i)=1; % replace the 0 element in sNew with a 1
        end
    end
end

sFiltered=sNew.*s; % multiplying ones & zeros matrix with s to remove unwanted freqs

wavplay(sFiltered,Fs); % playing the resulting filtered sound

But this code doesn't work. When I run the code it takes forever and freezes, and there is no output. What is wrong here?

Any help with fixing this code is greatly appreciated.
 
Last edited:
Physics news on Phys.org
On anything that freezes, look hard at any 'while' statement to make sure it is not getting stuck in there. You can put some displays inside the loops to see what is happening. That being said, Matlab is VERY slow at programmed loops like that, so it might just be taking a long time.
 
  • Like
Likes   Reactions: roam
Your code produces no output because you have written your while loop in such a way that it easily goes infinite. Look at the relationship between the loop conditional and the inside of the loop! There is nothing inside the loop that could change the result of evaluating the conditional required to enter the loop.

It is like coding, while 2 is less than 10, subtract 2 from 5. This would produce an infinite loop because subtracting 2 from 5 cannot change 2 being less than 10. Your loop is similar. The inside of the loop cannot change the value of the conditional.

Another problem in your code is that you refer to the entire vector s in the while loop conditional statement. I think you mean to refer to just element i of s, like s(i). This will not fix the first problem I mentioned, but it will get you closer.
 
  • Like
Likes   Reactions: roam and FactChecker
mfig said:
Your code produces no output because you have written your while loop in such a way that it easily goes infinite. Look at the relationship between the loop conditional and the inside of the loop! There is nothing inside the loop that could change the result of evaluating the conditional required to enter the loop.

It is like coding, while 2 is less than 10, subtract 2 from 5. This would produce an infinite loop because subtracting 2 from 5 cannot change 2 being less than 10. Your loop is similar. The inside of the loop cannot change the value of the conditional.

Another problem in your code is that you refer to the entire vector s in the while loop conditional statement. I think you mean to refer to just element i of s, like s(i). This will not fix the first problem I mentioned, but it will get you closer.

Thank you so much for the hint, it helped me a lot.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
6K
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 11 ·
Replies
11
Views
4K
  • · Replies 29 ·
Replies
29
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 2 ·
Replies
2
Views
26K
  • · Replies 5 ·
Replies
5
Views
2K