How to fix following error?

Hello Seniors, How to Solve following error I tried but failed to solve it.
t=0:0.300:0.050:0500; x=chirp (t,0,1,150); F=0:0.1:100; [y,f,t,p]=spectrogram(x,256,250,F,1E3,'yaxis');
Error using welchparse>segment_info (line 88)The length of the segment cannot be greater than the length of the input signal.
I will provide full code if anyone need actually my problem is to get voice frequency in miliseconds.

4 Comments

When I run the code you provided, there is no error. The error message you shared indicates that the source of error is welchparse>segment_info (line 88) and this is not included in the code you provided. Please provide a minimal working examples that produces the error and provide the full copy-pasted error message.
Here is the picture
There is no picture. A screen shot isn't really useful (if that's what you were going to share). People can't copy/paste a screen shot into their matlab workspace.
Yasir Ali
Yasir Ali on 27 Mar 2019
Edited: Stephen23 on 27 Mar 2019
here is complete error and also see full code below ,in which I can get good result but when I do some variation in code it shows error.
>> t=0.400:0.050:0.500;
>> x=chirp(t,0,1,150);
>> F = 0:.1:100;
>> [y,f,t,p] = spectrogram(x,256,250,F,1E3,'yaxis');
Error using welchparse>segment_info (line 188)
The length of the segments cannot be greater than the length of the input signal.
Error in welchparse (line 32)
[L,noverlap,win] = segment_info(M,win,noverlap);
Error in spectrogram (line 172)
[x,nx,~,~,~,win,~,~,noverlap,~,~,options] = welchparse(x,esttype,varargin{:});
full code which works for frequeency spectrum in miliseconds.
t=0:0.001:2; % 2 secs @ 1kHz sample rate
x=chirp(t,0,1,150); % Start @ DC, cross 150Hz at t=1sec
F = 0:.1:100;
[y,f,t,p] = spectrogram(x,256,250,F,1E3,'yaxis');
% NOTE: This is the same as calling SPECTROGRAM with no outputs.
surf(t,f,10*log10(abs(p)),'EdgeColor','none');
axis xy; axis tight; colormap(jet); view(0,90);
xlabel('Time');
ylabel('Frequency (Hz)');
[y,fs]=audioread('filename5sec'.wav);
NW = round(fs*0.025);
[S,F,T,P] = spectrogram(y(:,1),NW,[],[],fs);
% Then to plot:
subplot(2,1,1),pcolor(T,F,log10(P)),
shading flat,
caxis([-6 0]+max(caxis))
colorbar
subplot(2,1,2)
semilogy(F,P(:,23))
title(sprintf('PSD at time: %f',T(23)))
xlabel('Frequency (Hz)')

Sign in to comment.

 Accepted Answer

Walter Roberson
Walter Roberson on 28 Mar 2019
You assign a vector to t before the code, but you name t as an output parameter in the spectrogram() call. If you then try to use t after that, it is now only 3 x 1 and you have a problem.
The problem is not the first run: the problem is overwriting t but still expecting t to have the same meaning.

9 Comments

So what to do now as I can get my require result?
But the same error is produced even when you remove the overwritten variable.
t=0.400:0.050:0.500;
x=chirp(t,0,1,150);
F = 0:.1:100;
spectrogram(x,256,250,F,1E3,'yaxis')
Error using welchparse>segment_info (line 224)
The length of the segments cannot be greater than the length of the input signal.
Error in welchparse (line 32)
[L,noverlap,win] = segment_info(M,win,noverlap);
Error in pspectrogram (line 34)
[x,nx,~,y,ny,win,~,~,noverlap,~,~,options] = welchparse(x,esttype,varargin{:});
Error in spectrogram (line 168)
pspectrogram({x},'spect',varargin{:});
thats why Iam worried dear Walter help to solve it plz
t=0.400:0.050:0.500;
designates only 3 times, which is not enough for your segment size of 256.
Some of your other posts in this Question use a longer t variable that does not have this problem.
Yasir Ali
Yasir Ali on 28 Mar 2019
Edited: Yasir Ali on 28 Mar 2019
My problem is to get voice frequency in mili seconds as I mentioned that I want to get voice frequency response in mili seconds above 0.300/0.400 need help for this problem
Milliseconds is 0.001 not 0.050 . If you had used
t=0.400:0.001:0.500;
then you would not have the difficulty.
But is it correct that you are only sampling at 1 kHz? Voice is more likely to be at 2 kHz or 8 kHz (8000 samples per second, each 8 bit.)
Dear @Walter 2 khz , My aim is to find difference between male and female voice frequency after than I will choose particular voice of male and through their frequency I wish to recognise that who is speaking. Your suggestions Guideline will highly appreciated, as you are most senior and I have not a good knowledge thats why I prefer to ask and learn from you seniors.
2 kHz is increment 1/2000 which is 0.0005
The general formula is:
time = (0:number_of_samples-1) / sampling_frequency

Sign in to comment.

More Answers (1)

Adam Danz
Adam Danz on 27 Mar 2019
"Error: The length of the segments cannot be greater than the length of the input signal."
When you're using miliseconds, your x input contains 2001 samples and your window value of 256 breaks up the 2001 values into 256 segments. That works fine. The reason your code is breaking with the other set of parameters is that your x input only contains 3 samples but your window value is still 256. As the error message indicates, the number of segments can't be greater than the number of samples in x.

5 Comments

Dear Adam Danz Can you edit the code as it can run as for the 0.400 ms .and how to increase number of samples in x?
@Yasir, Walter and I both came to the same conclusion as to the source of the error. To fix the error, your t vector needs more samples. I don't know enough about your data to make a recommendation.
I understand you want your t vector to be in ms resolution. But I can't suggest how to convert whatever you've got into ms resolution without knowing more about the data.
I'm really against proposing solutions that merely prevent an error message from appearing. I think that's a really really bad approach at problem solving and data analysis. The better approach is to figure out what your code is supposed to be doing, what the variables are supposed to represent, and basing the troubleshooting off of that.
So, forget about the error for a moment. What is your t value supposed to represent? What do you think its bounds should be (it's endpoints)? How many values do you think it should have (roughly)? What does the variable represent?
Yasir Ali
Yasir Ali on 28 Mar 2019
Edited: Yasir Ali on 28 Mar 2019
@Adam, actually Iam not export using matlab and I have not a good knowledge to solve the problems thats why Iam trying to learn from seniors Like you and Walter. In t vector I assign values for miliseconds that when I assign my voice data it show the result which I assign in t vector, further Iam trying to solve problem as I mentioned above voice frequency in mili seconds I also share full code in which I also read my voice and ploted there frequency spectrum in miliseconds I dont know wether it is a fine way or not, dear Adam my data is simple that is five seconds wav file in which I want to break the five seconds wav file and want to plot frequency spectrum in miliseconds .
Further looking forward to hear a beautiful reply from you, your comments are highly appreciated.
Walter is a senior in this forum. I'm a novice compared to him :D
INow I understand that your data have a duration of 5 seconds and that you want the time variable to be at ms resolution. One more bit of information is missing: the interval of your data or the number of samples.
For example
t = 0:0.001:5;
length(t) % = 5001
gives you 5 seconds at 1ms intervals while
t = 0:0.005:5;
length(t) % = 1001
gives you 5 seconds at 5 ms intervals.
Or maybe you want 501 samples between 0 and 5 sec. That would be
linspace(0,5,501)
For now don't worry about being new at Matlab. I think the more important step is to be able to explain what you're trying to do (without worrying how to do it, for now).
Dear@Adam thank u for your comment, My main object is to compare frequencies of male and female voices I want to find out difference between male and female voices through frequencies after than I will recognise particular male voice by assigning frequency.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!