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

In summary: I fixed the while loop and now my code works perfectly. I also changed the conditional statement to refer to just element i of s. Thank you once again for your help. In summary, the conversation was about a person wanting to write a Matlab code that removes all frequency components from a .wav sound file except those within ±25 Hz of 523 Hz and its harmonics, without using any built-in filters. The person shared their code and asked for help as it was not producing any output and was freezing. The expert pointed out that the while loop in the code was causing an infinite loop and that the conditional statement should refer to just element i of s. The expert's suggestions helped the person fix their code and it now works perfectly.
  • #1
roam
1,271
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
  • #2
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 roam
  • #3
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 roam and FactChecker
  • #4
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.
 

1. What is the purpose of filtering frequencies in Matlab?

The purpose of filtering frequencies in Matlab is to remove unwanted noise or signals from a dataset, allowing for a clearer representation of the desired signal or data.

2. What is the difference between built-in filters and fixing filtering frequencies in Matlab?

Built-in filters are pre-programmed functions within Matlab that can be easily applied to a dataset. Fixing filtering frequencies in Matlab involves manually coding a filter using mathematical equations and algorithms.

3. How do I fix filtering frequencies in Matlab without using built-in filters?

To fix filtering frequencies without using built-in filters, you can use the "filter" function in Matlab, which allows you to specify your own filter coefficients. You can also use the "conv" function to perform convolution between your signal and a filter kernel.

4. What are some common mistakes when fixing filtering frequencies in Matlab?

Some common mistakes when fixing filtering frequencies in Matlab include using incorrect filter coefficients, not applying the filter to the correct data, and not considering the sampling rate or frequency of the data.

5. Are there any tips for optimizing filtering frequencies code in Matlab?

Some tips for optimizing filtering frequencies code in Matlab include using efficient algorithms, minimizing the number of filter coefficients, and using vectorization to speed up the filtering process. It is also important to test and fine-tune the filter parameters to achieve the desired result.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
126
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
29
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • Electrical Engineering
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
25K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
Back
Top