Prev: spectrogram
Next: mixed model anova
From: Wayne King on 10 May 2010 09:40 "Martin " <martin.bruha(a)gmail.com> wrote in message <hs8v8p$haa$1(a)fred.mathworks.com>... > "Wayne King" <wmkingty(a)gmail.com> wrote in message <hs8l6b$q30$1(a)fred.mathworks.com>... > > "Martin " <martin.bruha(a)gmail.com> wrote in message <hs8hlb$ca0$1(a)fred.mathworks.com>... > > > Hi Wayne, > > > > > > yes, I am plotting the spectrogram by calling the function with no output arguments. I also tried it with output arguments but then I get only the values and not the diagram (spectrogram), don't I? > > > The explanation with the dB makes sence, it was also one of my ideas. > > > I tried to specify the frequency range as you recommended. > > > I am interested only in the range up to 60 Hz so the following syntax: > > > > > > spectrogram(dc_current,2e4,18e3,2:0.01:60,4e4,'yaxis') > > > > > > I have now the range of frequencies 2...60 Hz with a step of 0.01 Hz. But the spectrogram has the resolution of about 2 Hz. What is the reason? I have to do something wrong... I need the frequencies for resonance investigation and such a resolution doesn't tell me anything... > > > Do you have any idea? It would help me a lot. > > > > > > > > > "Wayne King" <wmkingty(a)gmail.com> wrote in message <hs79s3$ng9$1(a)fred.mathworks.com>... > > > > > "Wayne King" <wmkingty(a)gmail.com> wrote in message <hs4ias$7a$1(a)fred.mathworks.com>... > > > > > > "Martin " <martin.bruha(a)gmail.com> wrote in message <hs4hih$bn6$1(a)fred.mathworks.com>... > > > > > > > Hi everyone > > > > > > > > > > > > > > I need a help with 'spectrogram' function. It makes FFT of my signal and plots it with time and frequency on the axis and the magnitude of frequency showed with color. > > > > > > > The frequency range of FFT algorithm depends on the sampling frequency. > > > > > > > In my case the sampling frequency is very high (160 kHz), but as the result I focus just on the range 0-60 Hz. Can I modify spectrogram to compute the fft only up to 60 Hz? > > > > > > > I can change the range in Axes properties but in this way I loose the information of magnitude (displayed with color) because I just cut a very small piece from wide frequency range > > > > > > > > > > > > > > Thank you for your help and tips. > > > > > > > > > > > > > > Martin Bruha > > > > > > > > > > > > Hi Martin, you can use the syntax > > > > > > > > > > > > [S,F,T]=spectrogram(x,window,noverlap,F,fs) > > > > > > > > > > > > See the documentation. You can specify a vector of frequencies,F, at which to compute the short-time Fourier transform. > > > > > > > > > > > > Also, you can always manipulate the colormap, or use caxis() so that you color your plot the way you wish. > > > > > > > > > > > > Hope that helps, > > > > > > Wayne > > > > "Martin " <martin.bruha(a)gmail.com> wrote in message <hs6ui8$1em$1(a)fred.mathworks.com>... > > > > > Hi Waine > > > > > thanks for the tips, I will check it now. Maybe one more question: I get the diagram using spectrogram function and then in the options I choose to add a colorbar. I wonder why about 60% of the range in colorbar is negative (like -100...+60). The color represents the magnitude of certain frequency component at certain time. How can it be negative? Or is there something wrong in my point of view? > > > > > Thanks again, > > > > > Martin > > > > > > > > > Hi Martin, Are you plotting the spectrogram by calling the function with no output arguments? If so, you are seeing that the magnitudes are plotted in dB, in which case magnitudes less than 1 result in negative values. > > > > > > > > Wayne > > > > Hi Martin, the frequency resolution of the spectrogram depends on the number of points and the sampling frequency. Since you have specified your segment length as 20,000 and your sampling frequency as 40,000, your frequency resolution is 2 Hz. There is an inevitable trade off between time and frequency resolution: the longer your segment length, the better the frequency resolution, but within each segment you lose all time resolution. Therefore the shorter the segment length, the better the time resolution, but the poorer the frequency resolution. If you require better frequency resolution, you will need to increase your segment length, but you will lose time resolution. Then specify your vector of frequencies accordingly. If you know your frequency resolution is 1 Hz (say you have doubled your segment length to 40,000 points), then specify your frequencies: > > > > f = 2:60; > > > > Hope that helps, > > Wayne > > Ok, I start to understand it. Unfortunately I have still 2 more problems: > 1) the first one is that I need to adjust the colors in the spectrogram diagram. I tried to use the caxis() command as you recommended. I noticed that by changing the cmin and cmax in caxis([cmin,cmax]) I can change the colors of the point in spectrogram but the range in the colorbar remains the same. So I understand it that certain point changes the color but also the magnitude which destroys the original information from the recorded signal. > 2) the other problem is that I would prefer to have the magnitude axis linear instead of log. Could you advise me how to do it? > Thank you so much for the huge help so far. > Martin Hi Martin, If you read the documentation for spectrogram, you will see that you can output the short-time Fourier transform as a complex-valued matrix, and if you choose, the matrix of short-time power spectral density estimates as well. You can plot either the absolute values of the complex-valued matrix, or the matrix of power spectral density estimates without using dB if you choose. t = 0:0.001:2; x = chirp(t,0,1,150); F = 0:.1:100; [y,f,t,p] = spectrogram(x,256,250,F,1E3,'yaxis'); [y,f,t,p] = spectrogram(x,256,250,F,1E3,'yaxis'); image(t,f,abs(y),'CDataMapping','scaled'); axis xy; Wayne
From: Martin on 10 May 2010 14:57 "Wayne King" <wmkingty(a)gmail.com> wrote in message <hs92c6$ap2$1(a)fred.mathworks.com>... > "Martin " <martin.bruha(a)gmail.com> wrote in message <hs8v8p$haa$1(a)fred.mathworks.com>... > > "Wayne King" <wmkingty(a)gmail.com> wrote in message <hs8l6b$q30$1(a)fred.mathworks.com>... > > > "Martin " <martin.bruha(a)gmail.com> wrote in message <hs8hlb$ca0$1(a)fred.mathworks.com>... > > > > Hi Wayne, > > > > > > > > yes, I am plotting the spectrogram by calling the function with no output arguments. I also tried it with output arguments but then I get only the values and not the diagram (spectrogram), don't I? > > > > The explanation with the dB makes sence, it was also one of my ideas. > > > > I tried to specify the frequency range as you recommended. > > > > I am interested only in the range up to 60 Hz so the following syntax: > > > > > > > > spectrogram(dc_current,2e4,18e3,2:0.01:60,4e4,'yaxis') > > > > > > > > I have now the range of frequencies 2...60 Hz with a step of 0.01 Hz. But the spectrogram has the resolution of about 2 Hz. What is the reason? I have to do something wrong... I need the frequencies for resonance investigation and such a resolution doesn't tell me anything... > > > > Do you have any idea? It would help me a lot. > > > > > > > > > > > > "Wayne King" <wmkingty(a)gmail.com> wrote in message <hs79s3$ng9$1(a)fred.mathworks.com>... > > > > > > "Wayne King" <wmkingty(a)gmail.com> wrote in message <hs4ias$7a$1(a)fred.mathworks.com>... > > > > > > > "Martin " <martin.bruha(a)gmail.com> wrote in message <hs4hih$bn6$1(a)fred.mathworks.com>... > > > > > > > > Hi everyone > > > > > > > > > > > > > > > > I need a help with 'spectrogram' function. It makes FFT of my signal and plots it with time and frequency on the axis and the magnitude of frequency showed with color. > > > > > > > > The frequency range of FFT algorithm depends on the sampling frequency. > > > > > > > > In my case the sampling frequency is very high (160 kHz), but as the result I focus just on the range 0-60 Hz. Can I modify spectrogram to compute the fft only up to 60 Hz? > > > > > > > > I can change the range in Axes properties but in this way I loose the information of magnitude (displayed with color) because I just cut a very small piece from wide frequency range > > > > > > > > > > > > > > > > Thank you for your help and tips. > > > > > > > > > > > > > > > > Martin Bruha > > > > > > > > > > > > > > Hi Martin, you can use the syntax > > > > > > > > > > > > > > [S,F,T]=spectrogram(x,window,noverlap,F,fs) > > > > > > > > > > > > > > See the documentation. You can specify a vector of frequencies,F, at which to compute the short-time Fourier transform. > > > > > > > > > > > > > > Also, you can always manipulate the colormap, or use caxis() so that you color your plot the way you wish. > > > > > > > > > > > > > > Hope that helps, > > > > > > > Wayne > > > > > "Martin " <martin.bruha(a)gmail.com> wrote in message <hs6ui8$1em$1(a)fred.mathworks.com>... > > > > > > Hi Waine > > > > > > thanks for the tips, I will check it now. Maybe one more question: I get the diagram using spectrogram function and then in the options I choose to add a colorbar. I wonder why about 60% of the range in colorbar is negative (like -100...+60). The color represents the magnitude of certain frequency component at certain time. How can it be negative? Or is there something wrong in my point of view? > > > > > > Thanks again, > > > > > > Martin > > > > > > > > > > > Hi Martin, Are you plotting the spectrogram by calling the function with no output arguments? If so, you are seeing that the magnitudes are plotted in dB, in which case magnitudes less than 1 result in negative values. > > > > > > > > > > Wayne > > > > > > Hi Martin, the frequency resolution of the spectrogram depends on the number of points and the sampling frequency. Since you have specified your segment length as 20,000 and your sampling frequency as 40,000, your frequency resolution is 2 Hz. There is an inevitable trade off between time and frequency resolution: the longer your segment length, the better the frequency resolution, but within each segment you lose all time resolution. Therefore the shorter the segment length, the better the time resolution, but the poorer the frequency resolution. If you require better frequency resolution, you will need to increase your segment length, but you will lose time resolution. Then specify your vector of frequencies accordingly. If you know your frequency resolution is 1 Hz (say you have doubled your segment length to 40,000 points), then specify your frequencies: > > > > > > f = 2:60; > > > > > > Hope that helps, > > > Wayne > > > > Ok, I start to understand it. Unfortunately I have still 2 more problems: > > 1) the first one is that I need to adjust the colors in the spectrogram diagram. I tried to use the caxis() command as you recommended. I noticed that by changing the cmin and cmax in caxis([cmin,cmax]) I can change the colors of the point in spectrogram but the range in the colorbar remains the same. So I understand it that certain point changes the color but also the magnitude which destroys the original information from the recorded signal. > > 2) the other problem is that I would prefer to have the magnitude axis linear instead of log. Could you advise me how to do it? > > Thank you so much for the huge help so far. > > Martin > > Hi Martin, If you read the documentation for spectrogram, you will see that you can output the short-time Fourier transform as a complex-valued matrix, and if you choose, the matrix of short-time power spectral density estimates as well. You can plot either the absolute values of the complex-valued matrix, or the matrix of power spectral density estimates without using dB if you choose. > > t = 0:0.001:2; > x = chirp(t,0,1,150); > F = 0:.1:100; > [y,f,t,p] = spectrogram(x,256,250,F,1E3,'yaxis'); > [y,f,t,p] = spectrogram(x,256,250,F,1E3,'yaxis'); > image(t,f,abs(y),'CDataMapping','scaled'); axis xy; > > > Wayne Hi Wayne, thank you once more for your advice. Now I can get the linear scaling but I guess that the values (magnitudes) are not correct. Here is a simple example: t = 0:0.001:2; x = chirp(t,0,1,150); F = 0:.1:100; case1) spectrogram(x,256,250,F,1E3,'yaxis'); case 2) [y,f,t,p] = spectrogram(x,256,250,F,1E3,'yaxis'); image(t,f,10*log10(abs(y)),'CDataMapping','scaled'); axis xy; In both cases I get the same picture, they both are log scaled. But if I compare the range on the log axis (colorbar) the values are different. Do I do a mistake? In my opinion in case1) the results are approximately what I expected but in case2) it seems to be shifted on the log axes. I am really confused now. Besides that I would have perfect results already. Could you help me once more please? Martin
From: Wayne King on 10 May 2010 19:46 "Martin " <martin.bruha(a)gmail.com> wrote in message <hs9ku1$8k8$1(a)fred.mathworks.com>... > "Wayne King" <wmkingty(a)gmail.com> wrote in message <hs92c6$ap2$1(a)fred.mathworks.com>... > > "Martin " <martin.bruha(a)gmail.com> wrote in message <hs8v8p$haa$1(a)fred.mathworks.com>... > > > "Wayne King" <wmkingty(a)gmail.com> wrote in message <hs8l6b$q30$1(a)fred.mathworks.com>... > > > > "Martin " <martin.bruha(a)gmail.com> wrote in message <hs8hlb$ca0$1(a)fred.mathworks.com>... > > > > > Hi Wayne, > > > > > > > > > > yes, I am plotting the spectrogram by calling the function with no output arguments. I also tried it with output arguments but then I get only the values and not the diagram (spectrogram), don't I? > > > > > The explanation with the dB makes sence, it was also one of my ideas. > > > > > I tried to specify the frequency range as you recommended. > > > > > I am interested only in the range up to 60 Hz so the following syntax: > > > > > > > > > > spectrogram(dc_current,2e4,18e3,2:0.01:60,4e4,'yaxis') > > > > > > > > > > I have now the range of frequencies 2...60 Hz with a step of 0.01 Hz. But the spectrogram has the resolution of about 2 Hz. What is the reason? I have to do something wrong... I need the frequencies for resonance investigation and such a resolution doesn't tell me anything... > > > > > Do you have any idea? It would help me a lot. > > > > > > > > > > > > > > > "Wayne King" <wmkingty(a)gmail.com> wrote in message <hs79s3$ng9$1(a)fred.mathworks.com>... > > > > > > > "Wayne King" <wmkingty(a)gmail.com> wrote in message <hs4ias$7a$1(a)fred.mathworks.com>... > > > > > > > > "Martin " <martin.bruha(a)gmail.com> wrote in message <hs4hih$bn6$1(a)fred.mathworks.com>... > > > > > > > > > Hi everyone > > > > > > > > > > > > > > > > > > I need a help with 'spectrogram' function. It makes FFT of my signal and plots it with time and frequency on the axis and the magnitude of frequency showed with color. > > > > > > > > > The frequency range of FFT algorithm depends on the sampling frequency. > > > > > > > > > In my case the sampling frequency is very high (160 kHz), but as the result I focus just on the range 0-60 Hz. Can I modify spectrogram to compute the fft only up to 60 Hz? > > > > > > > > > I can change the range in Axes properties but in this way I loose the information of magnitude (displayed with color) because I just cut a very small piece from wide frequency range > > > > > > > > > > > > > > > > > > Thank you for your help and tips. > > > > > > > > > > > > > > > > > > Martin Bruha > > > > > > > > > > > > > > > > Hi Martin, you can use the syntax > > > > > > > > > > > > > > > > [S,F,T]=spectrogram(x,window,noverlap,F,fs) > > > > > > > > > > > > > > > > See the documentation. You can specify a vector of frequencies,F, at which to compute the short-time Fourier transform. > > > > > > > > > > > > > > > > Also, you can always manipulate the colormap, or use caxis() so that you color your plot the way you wish. > > > > > > > > > > > > > > > > Hope that helps, > > > > > > > > Wayne > > > > > > "Martin " <martin.bruha(a)gmail.com> wrote in message <hs6ui8$1em$1(a)fred.mathworks.com>... > > > > > > > Hi Waine > > > > > > > thanks for the tips, I will check it now. Maybe one more question: I get the diagram using spectrogram function and then in the options I choose to add a colorbar. I wonder why about 60% of the range in colorbar is negative (like -100...+60). The color represents the magnitude of certain frequency component at certain time. How can it be negative? Or is there something wrong in my point of view? > > > > > > > Thanks again, > > > > > > > Martin > > > > > > > > > > > > > Hi Martin, Are you plotting the spectrogram by calling the function with no output arguments? If so, you are seeing that the magnitudes are plotted in dB, in which case magnitudes less than 1 result in negative values. > > > > > > > > > > > > Wayne > > > > > > > > Hi Martin, the frequency resolution of the spectrogram depends on the number of points and the sampling frequency. Since you have specified your segment length as 20,000 and your sampling frequency as 40,000, your frequency resolution is 2 Hz. There is an inevitable trade off between time and frequency resolution: the longer your segment length, the better the frequency resolution, but within each segment you lose all time resolution. Therefore the shorter the segment length, the better the time resolution, but the poorer the frequency resolution. If you require better frequency resolution, you will need to increase your segment length, but you will lose time resolution. Then specify your vector of frequencies accordingly. If you know your frequency resolution is 1 Hz (say you have doubled your segment length to 40,000 points), then specify your frequencies: > > > > > > > > f = 2:60; > > > > > > > > Hope that helps, > > > > Wayne > > > > > > Ok, I start to understand it. Unfortunately I have still 2 more problems: > > > 1) the first one is that I need to adjust the colors in the spectrogram diagram. I tried to use the caxis() command as you recommended. I noticed that by changing the cmin and cmax in caxis([cmin,cmax]) I can change the colors of the point in spectrogram but the range in the colorbar remains the same. So I understand it that certain point changes the color but also the magnitude which destroys the original information from the recorded signal. > > > 2) the other problem is that I would prefer to have the magnitude axis linear instead of log. Could you advise me how to do it? > > > Thank you so much for the huge help so far. > > > Martin > > > > Hi Martin, If you read the documentation for spectrogram, you will see that you can output the short-time Fourier transform as a complex-valued matrix, and if you choose, the matrix of short-time power spectral density estimates as well. You can plot either the absolute values of the complex-valued matrix, or the matrix of power spectral density estimates without using dB if you choose. > > > > t = 0:0.001:2; > > x = chirp(t,0,1,150); > > F = 0:.1:100; > > [y,f,t,p] = spectrogram(x,256,250,F,1E3,'yaxis'); > > [y,f,t,p] = spectrogram(x,256,250,F,1E3,'yaxis'); > > image(t,f,abs(y),'CDataMapping','scaled'); axis xy; > > > > > > Wayne > > Hi Wayne, > thank you once more for your advice. Now I can get the linear scaling but I guess that the values (magnitudes) are not correct. > Here is a simple example: > > t = 0:0.001:2; > x = chirp(t,0,1,150); > F = 0:.1:100; > > case1) spectrogram(x,256,250,F,1E3,'yaxis'); > > case 2) [y,f,t,p] = spectrogram(x,256,250,F,1E3,'yaxis'); > image(t,f,10*log10(abs(y)),'CDataMapping','scaled'); axis xy; > > In both cases I get the same picture, they both are log scaled. But if I compare the range on the log axis (colorbar) the values are different. > Do I do a mistake? In my opinion in case1) the results are approximately what I expected but in case2) it seems to be shifted on the log axes. > I am really confused now. Besides that I would have perfect results already. > Could you help me once more please? > Martin Martin, The difference is in the second call you are using 'CDataMappling', 'scaled' That is not present in the plotting command used in the call to spectrogram with no outputs. Hope that helps, Wayne
From: Martin on 11 May 2010 02:49 "Wayne King" <wmkingty(a)gmail.com> wrote in message <hsa5sc$607$1(a)fred.mathworks.com>... > "Martin " <martin.bruha(a)gmail.com> wrote in message <hs9ku1$8k8$1(a)fred.mathworks.com>... > > "Wayne King" <wmkingty(a)gmail.com> wrote in message <hs92c6$ap2$1(a)fred.mathworks.com>... > > > "Martin " <martin.bruha(a)gmail.com> wrote in message <hs8v8p$haa$1(a)fred.mathworks.com>... > > > > "Wayne King" <wmkingty(a)gmail.com> wrote in message <hs8l6b$q30$1(a)fred.mathworks.com>... > > > > > "Martin " <martin.bruha(a)gmail.com> wrote in message <hs8hlb$ca0$1(a)fred.mathworks.com>... > > > > > > Hi Wayne, > > > > > > > > > > > > yes, I am plotting the spectrogram by calling the function with no output arguments. I also tried it with output arguments but then I get only the values and not the diagram (spectrogram), don't I? > > > > > > The explanation with the dB makes sence, it was also one of my ideas. > > > > > > I tried to specify the frequency range as you recommended. > > > > > > I am interested only in the range up to 60 Hz so the following syntax: > > > > > > > > > > > > spectrogram(dc_current,2e4,18e3,2:0.01:60,4e4,'yaxis') > > > > > > > > > > > > I have now the range of frequencies 2...60 Hz with a step of 0.01 Hz. But the spectrogram has the resolution of about 2 Hz. What is the reason? I have to do something wrong... I need the frequencies for resonance investigation and such a resolution doesn't tell me anything... > > > > > > Do you have any idea? It would help me a lot. > > > > > > > > > > > > > > > > > > "Wayne King" <wmkingty(a)gmail.com> wrote in message <hs79s3$ng9$1(a)fred.mathworks.com>... > > > > > > > > "Wayne King" <wmkingty(a)gmail.com> wrote in message <hs4ias$7a$1(a)fred.mathworks.com>... > > > > > > > > > "Martin " <martin.bruha(a)gmail.com> wrote in message <hs4hih$bn6$1(a)fred.mathworks.com>... > > > > > > > > > > Hi everyone > > > > > > > > > > > > > > > > > > > > I need a help with 'spectrogram' function. It makes FFT of my signal and plots it with time and frequency on the axis and the magnitude of frequency showed with color. > > > > > > > > > > The frequency range of FFT algorithm depends on the sampling frequency. > > > > > > > > > > In my case the sampling frequency is very high (160 kHz), but as the result I focus just on the range 0-60 Hz. Can I modify spectrogram to compute the fft only up to 60 Hz? > > > > > > > > > > I can change the range in Axes properties but in this way I loose the information of magnitude (displayed with color) because I just cut a very small piece from wide frequency range > > > > > > > > > > > > > > > > > > > > Thank you for your help and tips. > > > > > > > > > > > > > > > > > > > > Martin Bruha > > > > > > > > > > > > > > > > > > Hi Martin, you can use the syntax > > > > > > > > > > > > > > > > > > [S,F,T]=spectrogram(x,window,noverlap,F,fs) > > > > > > > > > > > > > > > > > > See the documentation. You can specify a vector of frequencies,F, at which to compute the short-time Fourier transform. > > > > > > > > > > > > > > > > > > Also, you can always manipulate the colormap, or use caxis() so that you color your plot the way you wish. > > > > > > > > > > > > > > > > > > Hope that helps, > > > > > > > > > Wayne > > > > > > > "Martin " <martin.bruha(a)gmail.com> wrote in message <hs6ui8$1em$1(a)fred.mathworks.com>... > > > > > > > > Hi Waine > > > > > > > > thanks for the tips, I will check it now. Maybe one more question: I get the diagram using spectrogram function and then in the options I choose to add a colorbar. I wonder why about 60% of the range in colorbar is negative (like -100...+60). The color represents the magnitude of certain frequency component at certain time. How can it be negative? Or is there something wrong in my point of view? > > > > > > > > Thanks again, > > > > > > > > Martin > > > > > > > > > > > > > > > Hi Martin, Are you plotting the spectrogram by calling the function with no output arguments? If so, you are seeing that the magnitudes are plotted in dB, in which case magnitudes less than 1 result in negative values. > > > > > > > > > > > > > > Wayne > > > > > > > > > > Hi Martin, the frequency resolution of the spectrogram depends on the number of points and the sampling frequency. Since you have specified your segment length as 20,000 and your sampling frequency as 40,000, your frequency resolution is 2 Hz. There is an inevitable trade off between time and frequency resolution: the longer your segment length, the better the frequency resolution, but within each segment you lose all time resolution. Therefore the shorter the segment length, the better the time resolution, but the poorer the frequency resolution. If you require better frequency resolution, you will need to increase your segment length, but you will lose time resolution. Then specify your vector of frequencies accordingly. If you know your frequency resolution is 1 Hz (say you have doubled your segment length to 40,000 points), then specify your frequencies: > > > > > > > > > > f = 2:60; > > > > > > > > > > Hope that helps, > > > > > Wayne > > > > > > > > Ok, I start to understand it. Unfortunately I have still 2 more problems: > > > > 1) the first one is that I need to adjust the colors in the spectrogram diagram. I tried to use the caxis() command as you recommended. I noticed that by changing the cmin and cmax in caxis([cmin,cmax]) I can change the colors of the point in spectrogram but the range in the colorbar remains the same. So I understand it that certain point changes the color but also the magnitude which destroys the original information from the recorded signal. > > > > 2) the other problem is that I would prefer to have the magnitude axis linear instead of log. Could you advise me how to do it? > > > > Thank you so much for the huge help so far. > > > > Martin > > > > > > Hi Martin, If you read the documentation for spectrogram, you will see that you can output the short-time Fourier transform as a complex-valued matrix, and if you choose, the matrix of short-time power spectral density estimates as well. You can plot either the absolute values of the complex-valued matrix, or the matrix of power spectral density estimates without using dB if you choose. > > > > > > t = 0:0.001:2; > > > x = chirp(t,0,1,150); > > > F = 0:.1:100; > > > [y,f,t,p] = spectrogram(x,256,250,F,1E3,'yaxis'); > > > [y,f,t,p] = spectrogram(x,256,250,F,1E3,'yaxis'); > > > image(t,f,abs(y),'CDataMapping','scaled'); axis xy; > > > > > > > > > Wayne > > > > Hi Wayne, > > thank you once more for your advice. Now I can get the linear scaling but I guess that the values (magnitudes) are not correct. > > Here is a simple example: > > > > t = 0:0.001:2; > > x = chirp(t,0,1,150); > > F = 0:.1:100; > > > > case1) spectrogram(x,256,250,F,1E3,'yaxis'); > > > > case 2) [y,f,t,p] = spectrogram(x,256,250,F,1E3,'yaxis'); > > image(t,f,10*log10(abs(y)),'CDataMapping','scaled'); axis xy; > > > > In both cases I get the same picture, they both are log scaled. But if I compare the range on the log axis (colorbar) the values are different. > > Do I do a mistake? In my opinion in case1) the results are approximately what I expected but in case2) it seems to be shifted on the log axes. > > I am really confused now. Besides that I would have perfect results already. > > Could you help me once more please? > > Martin > > Martin, > > The difference is in the second call you are using 'CDataMappling', 'scaled' > That is not present in the plotting command used in the call to spectrogram with no outputs. > > Hope that helps, > Wayne Hi Wayne, I tried the image command without the setting 'CDataMappling' and 'scaled' and the magnitudes are still different compared to the values I get from spectrogram. t = 0:0.001:2; x = chirp(t,0,1,150); F = 0:.1:100; [y,f,t,p] = spectrogram(x,256,250,F,1E3,'yaxis'); spectrogram(x,256,250,F,1E3,'yaxis'); image(t,f,10*log10(abs(y))); axis xy; spectrogram() gives the highest magnitude about -20 dB, image() nearly +20 dB. That is the reason why I cannot use image for linear 'y' unless I modify it in some way so that the magnitudes fit. Can you recognize the problem? Thanks, Martin
From: Wayne King on 11 May 2010 05:52
"Martin " <martin.bruha(a)gmail.com> wrote in message <hsaul0$cp2$1(a)fred.mathworks.com>... > "Wayne King" <wmkingty(a)gmail.com> wrote in message <hsa5sc$607$1(a)fred.mathworks.com>... > > "Martin " <martin.bruha(a)gmail.com> wrote in message <hs9ku1$8k8$1(a)fred.mathworks.com>... > > > "Wayne King" <wmkingty(a)gmail.com> wrote in message <hs92c6$ap2$1(a)fred.mathworks.com>... > > > > "Martin " <martin.bruha(a)gmail.com> wrote in message <hs8v8p$haa$1(a)fred.mathworks.com>... > > > > > "Wayne King" <wmkingty(a)gmail.com> wrote in message <hs8l6b$q30$1(a)fred.mathworks.com>... > > > > > > "Martin " <martin.bruha(a)gmail.com> wrote in message <hs8hlb$ca0$1(a)fred.mathworks.com>... > > > > > > > Hi Wayne, > > > > > > > > > > > > > > yes, I am plotting the spectrogram by calling the function with no output arguments. I also tried it with output arguments but then I get only the values and not the diagram (spectrogram), don't I? > > > > > > > The explanation with the dB makes sence, it was also one of my ideas. > > > > > > > I tried to specify the frequency range as you recommended. > > > > > > > I am interested only in the range up to 60 Hz so the following syntax: > > > > > > > > > > > > > > spectrogram(dc_current,2e4,18e3,2:0.01:60,4e4,'yaxis') > > > > > > > > > > > > > > I have now the range of frequencies 2...60 Hz with a step of 0.01 Hz. But the spectrogram has the resolution of about 2 Hz. What is the reason? I have to do something wrong... I need the frequencies for resonance investigation and such a resolution doesn't tell me anything... > > > > > > > Do you have any idea? It would help me a lot. > > > > > > > > > > > > > > > > > > > > > "Wayne King" <wmkingty(a)gmail.com> wrote in message <hs79s3$ng9$1(a)fred.mathworks.com>... > > > > > > > > > "Wayne King" <wmkingty(a)gmail.com> wrote in message <hs4ias$7a$1(a)fred.mathworks.com>... > > > > > > > > > > "Martin " <martin.bruha(a)gmail.com> wrote in message <hs4hih$bn6$1(a)fred.mathworks.com>... > > > > > > > > > > > Hi everyone > > > > > > > > > > > > > > > > > > > > > > I need a help with 'spectrogram' function. It makes FFT of my signal and plots it with time and frequency on the axis and the magnitude of frequency showed with color. > > > > > > > > > > > The frequency range of FFT algorithm depends on the sampling frequency. > > > > > > > > > > > In my case the sampling frequency is very high (160 kHz), but as the result I focus just on the range 0-60 Hz. Can I modify spectrogram to compute the fft only up to 60 Hz? > > > > > > > > > > > I can change the range in Axes properties but in this way I loose the information of magnitude (displayed with color) because I just cut a very small piece from wide frequency range > > > > > > > > > > > > > > > > > > > > > > Thank you for your help and tips. > > > > > > > > > > > > > > > > > > > > > > Martin Bruha > > > > > > > > > > > > > > > > > > > > Hi Martin, you can use the syntax > > > > > > > > > > > > > > > > > > > > [S,F,T]=spectrogram(x,window,noverlap,F,fs) > > > > > > > > > > > > > > > > > > > > See the documentation. You can specify a vector of frequencies,F, at which to compute the short-time Fourier transform. > > > > > > > > > > > > > > > > > > > > Also, you can always manipulate the colormap, or use caxis() so that you color your plot the way you wish. > > > > > > > > > > > > > > > > > > > > Hope that helps, > > > > > > > > > > Wayne > > > > > > > > "Martin " <martin.bruha(a)gmail.com> wrote in message <hs6ui8$1em$1(a)fred.mathworks.com>... > > > > > > > > > Hi Waine > > > > > > > > > thanks for the tips, I will check it now. Maybe one more question: I get the diagram using spectrogram function and then in the options I choose to add a colorbar. I wonder why about 60% of the range in colorbar is negative (like -100...+60). The color represents the magnitude of certain frequency component at certain time. How can it be negative? Or is there something wrong in my point of view? > > > > > > > > > Thanks again, > > > > > > > > > Martin > > > > > > > > > > > > > > > > > Hi Martin, Are you plotting the spectrogram by calling the function with no output arguments? If so, you are seeing that the magnitudes are plotted in dB, in which case magnitudes less than 1 result in negative values. > > > > > > > > > > > > > > > > Wayne > > > > > > > > > > > > Hi Martin, the frequency resolution of the spectrogram depends on the number of points and the sampling frequency. Since you have specified your segment length as 20,000 and your sampling frequency as 40,000, your frequency resolution is 2 Hz. There is an inevitable trade off between time and frequency resolution: the longer your segment length, the better the frequency resolution, but within each segment you lose all time resolution. Therefore the shorter the segment length, the better the time resolution, but the poorer the frequency resolution. If you require better frequency resolution, you will need to increase your segment length, but you will lose time resolution. Then specify your vector of frequencies accordingly. If you know your frequency resolution is 1 Hz (say you have doubled your segment length to 40,000 points), then specify your frequencies: > > > > > > > > > > > > f = 2:60; > > > > > > > > > > > > Hope that helps, > > > > > > Wayne > > > > > > > > > > Ok, I start to understand it. Unfortunately I have still 2 more problems: > > > > > 1) the first one is that I need to adjust the colors in the spectrogram diagram. I tried to use the caxis() command as you recommended. I noticed that by changing the cmin and cmax in caxis([cmin,cmax]) I can change the colors of the point in spectrogram but the range in the colorbar remains the same. So I understand it that certain point changes the color but also the magnitude which destroys the original information from the recorded signal. > > > > > 2) the other problem is that I would prefer to have the magnitude axis linear instead of log. Could you advise me how to do it? > > > > > Thank you so much for the huge help so far. > > > > > Martin > > > > > > > > Hi Martin, If you read the documentation for spectrogram, you will see that you can output the short-time Fourier transform as a complex-valued matrix, and if you choose, the matrix of short-time power spectral density estimates as well. You can plot either the absolute values of the complex-valued matrix, or the matrix of power spectral density estimates without using dB if you choose. > > > > > > > > t = 0:0.001:2; > > > > x = chirp(t,0,1,150); > > > > F = 0:.1:100; > > > > [y,f,t,p] = spectrogram(x,256,250,F,1E3,'yaxis'); > > > > [y,f,t,p] = spectrogram(x,256,250,F,1E3,'yaxis'); > > > > image(t,f,abs(y),'CDataMapping','scaled'); axis xy; > > > > > > > > > > > > Wayne > > > > > > Hi Wayne, > > > thank you once more for your advice. Now I can get the linear scaling but I guess that the values (magnitudes) are not correct. > > > Here is a simple example: > > > > > > t = 0:0.001:2; > > > x = chirp(t,0,1,150); > > > F = 0:.1:100; > > > > > > case1) spectrogram(x,256,250,F,1E3,'yaxis'); > > > > > > case 2) [y,f,t,p] = spectrogram(x,256,250,F,1E3,'yaxis'); > > > image(t,f,10*log10(abs(y)),'CDataMapping','scaled'); axis xy; > > > > > > In both cases I get the same picture, they both are log scaled. But if I compare the range on the log axis (colorbar) the values are different. > > > Do I do a mistake? In my opinion in case1) the results are approximately what I expected but in case2) it seems to be shifted on the log axes. > > > I am really confused now. Besides that I would have perfect results already. > > > Could you help me once more please? > > > Martin > > > > Martin, > > > > The difference is in the second call you are using 'CDataMappling', 'scaled' > > That is not present in the plotting command used in the call to spectrogram with no outputs. > > > > Hope that helps, > > Wayne > > Hi Wayne, > I tried the image command without the setting 'CDataMappling' and 'scaled' and the magnitudes are still different compared to the values I get from spectrogram. > > t = 0:0.001:2; > x = chirp(t,0,1,150); > F = 0:.1:100; > [y,f,t,p] = spectrogram(x,256,250,F,1E3,'yaxis'); > spectrogram(x,256,250,F,1E3,'yaxis'); > image(t,f,10*log10(abs(y))); axis xy; > > spectrogram() gives the highest magnitude about -20 dB, image() nearly +20 dB. > That is the reason why I cannot use image for linear 'y' unless I modify it in some way so that the magnitudes fit. > Can you recognize the problem? > Thanks, > Martin Hi Martin, sorry I missed in the previous example that spectrogram with no output displays the short-time PSD estimates, so t = 0:0.001:2; x = chirp(t,0,1,150); F = 0:.1:100; [y,f,t,p] = spectrogram(x,256,250,F,1E3,'yaxis'); spectrogram(x,256,250,F,1E3,'yaxis'); colorbar; figure; imagesc(t,f,10*log10(p)); colorbar; axis xy; Wayne |