You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Designfilt and crossover filters?
1 view (last 30 days)
Show older comments
Can anybody help me with an alternative function for DesignFilt and crossover filters. These functions work only with matlab 2016. it doesn't work in matlab 2013. What is the alternative function?
Accepted Answer
Star Strider
on 16 Mar 2017
8 Comments
Darsana P M
on 16 Mar 2017
Thanks for this. But my problem is that an error occurs for 'designfilt' in the code, since i am using matlab 2013 version. What should i do?
if true
% [A,B,C,D] = cheby2(10,40,[500 560]/750);
d = designfilt('bandpassiir','FilterOrder',20, ...
'StopbandFrequency1',500,'StopbandFrequency2',560, ...
'StopbandAttenuation',40,'SampleRate',1500);
sos = ss2sos(A,B,C,D);
fvt = fvtool(sos,d,'Fs',1500);
legend(fvt,'cheby2','designfilt')
end
Star Strider
on 16 Mar 2017
I no longer have access to those functions (in R2017a). Since you want to design a bandpass filter, one option is the fdesign.bandpass function.
You can also design it yourself.
Example (using your design criteria):
Fs = 1500; % Sampling Frequency
Fn = Fs/2; % Nyquist Frequency
Wp = [500 560]/Fn; % Passband Frequencies (Normalised)
Ws = [495 565]/Fn; % Stopband Frequencies (Normalised)
Rp = 5; % Passband Ripple (dB)
Rs = 50; % Stopband Ripple (dB)
[n,Ws] = cheb2ord(Wp,Ws,Rp,Rs); % Calculate Filter Order
[z,p,k] = cheby2(n,Rs,Ws); % Design Filter
[sos,g] = zp2sos(z,p,k); % Convert To Second-Order-Section For Stability
figure(1)
freqz(sos, 2^16, Fs)
set(subplot(2,1,1), 'XLim',[0 700]) % ‘Zoom’ X-Axis
set(subplot(2,1,1), 'YLim',[-50 100]) % ‘Zoom’ Y-Axis (Amplitude)
set(subplot(2,1,2), 'XLim',[0 700]) % ‘Zoom’ X-Axis
Darsana P M
on 17 Mar 2017
Edited: Star Strider
on 17 Mar 2017
That really helped me.Thanks a lot. But a small doubt,
freqz(sos, 2^16, Fs)
set(subplot(2,1,1), 'XLim',[0 700]) % ‘Zoom’ X-Axis
set(subplot(2,1,1), 'YLim',[-50 100]) % ‘Zoom’ Y-Axis (Amplitude)
set(subplot(2,1,2), 'XLim',[0 700]) % ‘Zoom’ X-Axis
Why are these functions used??
Star Strider
on 17 Mar 2017
My pleasure.
Those let you see that the filter design is what you want. They are not necessary for the code or for the actual filtering, but very helpful and will save you significant time because you can see what your filter is actually doing in frequency space.
Darsana P M
on 17 Mar 2017
Thanks a lot. In a similar fashion if i want to design it for a bank of filters in the same graph itself, what will i do?
If suppose an audio signal is given, i have to design a band pass filter. The audio signal must be divided into sub bands ie 10-20Khz,20-30khz and so on. What can i do??
Star Strider
on 17 Mar 2017
My pleasure.
See the techniques in How to shape the spectrum of an audio waveform?. It will be straightforward to adapt that code to your specifications. It designs a bank of Butterworth filters, and you can easily adapt it to Chebyshev Type II filters. It will probably be worthwhile to use the cheb2ord function in the loop to arrive at the correct order.
Remember to save your ‘sos’ and ‘g’ filter coefficients cell arrays to a ‘.mat’ file so you can simply load them rather than having to recalculate them when you are satisfied with their design and performance.
Darsana P M
on 17 Mar 2017
Edited: Star Strider
on 17 Mar 2017
Can you please explain it once again. Is this the code that i must use?? But i got an error in the for loop part??
Fs = 8200; % Samping Frequency (Hz)
Fn = Fs/2; % Nyquist Frequency
pf = linspace(20,4000,17); % Passband Frequencies
cf = pf(1:end-1)+(pf(2)-pf(1))/2; % Centre Frequencies
for k1 = 1:length(cf)
[z(k1,:),p(k1,:),k(k1)] = butter(7, [pf(k1) pf(k1+1)]/Fn);
[sos{k1},g{k1}] = zp2sos(z(k1,:),p(k1,:),k(k1));
[h(k1,:),w(k1,:)] = freqz(sos{k1},512,Fs);
end
figure(1)
plot(w([1 16],:), abs(h([1 16],:)))
grid
% axis([0 0.2 ylim])
figure(2)
freqz(sos{1})
hold on
for k1 = 2:16
freqz(sos{k1})
end
hold off
Star Strider
on 17 Mar 2017
You will need to change the code to design the filters that meet your requirements. Use your own sampling frequency, and define your filter passband frequencies in a vector and assign them to the ‘pf’ variable. You might be able to use the linspace call with only minor modifications. The rest of my code should work without any need to change it.
I ran that code just now and it ran for me without error (in R2017a, the code was written in R2015a). What error did you see? Please copy the entire red text of the error from the Command Window and paste it to a comment here.
More Answers (2)
Darsana P M
on 17 Mar 2017
Error using vertcat Dimensions of matrices being concatenated are not consistent.
Error in zp2sos>orderp (line 247) new_p = [p_conj;p_real];
Error in zp2sos (line 94) [new_p,p_conj,p_real] = orderp(p_conj,p_real);
Error in band8 (line 7) [sos{k1},g{k1}] = zp2sos(z(k1,:),p(k1,:),k(k1));
This is the error that i obtained. I tried it in matlab 2013a
10 Comments
Star Strider
on 17 Mar 2017
I no longer have access to R2013a. There should be no problem with the cell array assignments.
There could be differences in how the vectors are output. See if these changes work:
[z(:,k1),p(:,k1),k(k1)] = butter(7, [pf(k1) pf(k1+1)]/Fn);
[sos{k1},g{k1}] = zp2sos(z(:,k1),p(:,k1),k(k1));
It changes the row vectors to column vectors. This is a guess on my part as to how the R2013a butter (and other) functions format their outputs.
If that does not work, I am out of ideas.
Darsana P M
on 18 Mar 2017
Wow that really worked!!!!!!!!! Thanks a lot. I got the output. In this, the 16 bandpass filters, has its starting and ending point at 0 and 1. Which parameter should i change? I have attached an image. To obtain the graph, should i change the pf parameter??
And also, can i do give an audio signal as input?? Will i be able to get the output??
Star Strider
on 18 Mar 2017
‘Wow that really worked!!!!!!!!! Thanks a lot.’
Thank you! My pleasure!
‘Which parameter should i change?’
I am not certain what you want. You cannot have any of the passbands or stopbands exactly at 0 or 1 (actually normalised frequencies of 0 or pi or 0 and your Nyquist frequency) because those are the limits of the frequency space for your sampled signal. You can get close to them (you have to experiment with your filters to determine how close you can get, depending on the other filter parameters) but you cannot by definition include them. (They are included by default in lowpass and highpass filters that do not specifically include them. You cannot specify filter band limits that include them.)
So if you want a filter that goes from (and includes) 0 Hz to the lower passband of your lowest bandpass filter, use a lowpass filter design with the passband approaching the lower passband of your lowest filter. If you want a filter that goes from the upper passband of your highest filter to the Nyquist frequency, use a highpass filter with the passband just above the upper passband of your highest filter. That is a long reply to a short question. I wanted to provide background.
You can definitely filter your audio signal. If it is a (Nx2) stereo matrix, filter it as you would filter any other signal. Both channels will be filtered at the same time, and the output will be a (Nx2) array of your filtered signal.
It is easiest to filter your signal through each filter in a loop similar to the way you created them. Use the filtfilt function to do the actual filtering.
Example code to do the filtering:
for k1 = 1:length(cf)
filt_sig{k1} = filtfilt(sos{k1}, g{k1}, sig);
end
where ‘sig’ is the signal you are filtering. I use a cell array for ‘filt_sig’ because I do not know anything about your signal. The cell array will work for ‘sig’ being either a vector or a matrix (for example an (Nx2) stereo sound signal). You can use a double array, and assign the output to the appropriate rows or columns, but here, the cell array is easier. It is easy to recover your filtered signal from the cell array. The filtered output signal will be in the cell array.
Darsana P M
on 18 Mar 2017
Thanks a lot for the answer. I actually want to design a bandpass filter for a chochlear implant. ie 20hz -20khz must be divided into bank of filters. 100-200hz; 200-300hz and so on. I got confused as to which method to apply. What should I do?? I think that a graph similar to the image I have sent must be obtained.
Star Strider
on 18 Mar 2017
My pleasure.
All you need to do is to supply a vector of band-edge frequencies that meet your requirements.
This will design 200 filters matching your requirements:
pf = [20 100:100:2E+4];
Note — The length (number of elements) in ‘pf’ must be odd. The length of this ‘pf’ vector is 201, so it will work.
Darsana P M
on 18 Mar 2017
So, if I give pf = [20 100:100:2E+4]; in the above code, will i be able to get the result??
Star Strider
on 18 Mar 2017
Yes, providing that your sampling frequency is at least 44.1 kHz (the standard audio sampling frequency). That gives enough frequency space on the high end (from 20 kHz to 22.05 kHz) to allow the highest filter to have a complete passband, including the transition region from the passband to stopband.
Darsana P M
on 19 Mar 2017
Ok...Thanks a lot
Star Strider
on 19 Mar 2017
My pleasure.
Darsana P M
on 22 Mar 2017
Fs = 8200; % Samping Frequency (Hz)
Fn = Fs/2; % Nyquist Frequency
pf = [20 100:100:2E+4] % Passband Frequencies
cf = pf(1:end-1)+(pf(2)-pf(1))/2; % Centre Frequencies
for k1 = 1:length(cf)
[z(k1,:),p(k1,:),k(k1)] = butter(7, [pf(k1) pf(k1+1)]/Fn);
[sos{k1},g{k1}] = zp2sos(z(k1,:),p(k1,:),k(k1));
[h(k1,:),w(k1,:)] = freqz(sos{k1},512,Fs);
end
figure(1)
plot(w([1 16],:), abs(h([1 16],:)))
grid
% axis([0 0.2 ylim])
figure(2)
freqz(sos{1})
hold on
for k1 = 2:16
freqz(sos{k1})
end
hold off
I wrote the pf command as you said. pf=[20 100:100:2E+4]. But i got an error saying
the cut off frequencies must be within the interval (0,1); line 57
[z(k1,:),p(k1,:),k(k1)] = butter(7, [pf(k1) pf(k1+1)]/Fn); line 7
This was done to get a similar graph as shown in figure?? What will i do?
4 Comments
Star Strider
on 22 Mar 2017
As I mentioned before, your sampling frequency, ‘Fs’ is assumed to be the standard audio sampling frequency of 44100 Hz, giving a Nyquist frequency, ‘Fn’ of 22050 Hz. If you use that sampling frequency, you can design your filters with no problems.
You cannot design your filters with those passbands with a sampling frequency of 8200 Hz. It is simply not possible.
Darsana P M
on 24 Mar 2017
Thanks a lot
Star Strider
on 24 Mar 2017
My pleasure.
See Also
Categories
Find more on Measurements and Spatial Audio in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)

