Réponse acceptée

Star Strider
Star Strider le 20 Mar 2018

0 votes

Other peaks may exist, although if the constant, (or ‘d-c’) offset is large enough, you will not be able to see them.
Try this:
id = data(:,2); % to load current values from the second column of my .txt
i = id - mean(id);
That should reveal the other peaks.

8 commentaires

Patrik Soukup
Patrik Soukup le 20 Mar 2018
Thanks for answer, sir.
Tried, but unfortunately, didn´t help :(
You might be correct with the idea of existing, but invisible peaks, I gave you incorrect info, that peak is on f=50Hz (in the scale of 25kHz it looked like it was on zero, my bad), which is the foundamental frequency, so that seems reasonable.
Any other idea?
Thanks in advance.
Star Strider
Star Strider le 20 Mar 2018
My pleasure.
Using semilogy (or loglog) instead of plot would allow you to see the details.
Patrik Soukup
Patrik Soukup le 20 Mar 2018
Wow, that´s perfect, thank you very much, you helped me alot, sir!
Star Strider
Star Strider le 20 Mar 2018
As always, my pleasure!
Dear Star Strider, may I ask you another question please? I got a set of data from my FFT (magnitudes I assume) in log scale and now I would like to normalize the Y-axis and scale it from 0 (where 0 would represent the highest frequency from the time-domain signal) to -100. I tried to normalize it via the code below, but I got a scale from 0 to -1000.
>> load data.txt;
>> i=data(:,2); % to load current values from the second column of my .txt
>> fs=50000; %sampling frequency
>> T=1/fs;
>> L=9000000; %number of samples
>> t=(0:L-1)*T;
>> plot(fs*t,i);
>> nfft=2^nextpow2(L);
>> I=fft(i,nfft)/L;
>> f=fs/2*linspace(0,1,nfft/2+1);
>> Iref=max(I);
>> Inorm=20*log(I/Iref);
>> plot(f,2*(Inorm(1:nfft/2+1)));
The output I got is here:
If there was something like I forgot to divide by 10 or so (and thus the Y-axis would be already in dBs but scaled wrongly, due to the theory, my signal should be on like -80 up to -70 dB, not like -60 to -40.
Thanks in advance for any insight.
As always, my pleasure.
You can always ask. I will answer if I can, my knowledge not being infinite.
I cannot run your code to check it, however it appears to be correct. The -1000 lower y-axis limit seems to be the nature of your data. The output of fft is complex, so the first change I would do is to add:
Ia = abs(I);
then use ‘Ia’ in rest of your analysis and plot. That could produce the result you want. Otherwise, the simplest option (without distorting the data) would be to use set to define the y-axis limits:
set(gca, 'YLim',[-100,0])
This sets the scale in dB, since that is the y-axis plotting scale.
Note that I have not tested this. It should work.
Patrik Soukup
Patrik Soukup le 22 Mar 2018
If I use the absolute value of I, the plot is upside down then, instead of being scaled 0 up to -1000, its scaled 0 to 1000. I used to have this part of the code in my code as well, but it seemed like there´s no way of ploting absolute value of fft data scaled in negative values, such as -dB. Then if I set the limits of Y axis, it deform the data completely
Taking the absolute values first should not cause your data to plot upside down. Taking the absolute value is the correct way to calculate the spectrum and convert it to dB.
I described essentially this procedure in my earlier Comment:
Ia = abs(I);
Iref = max(Ia);
Inorm = 20*log10(Ia/Iref);
Note that decibels are defined with respect to the base 10 logarithm, not the natural logarithm. I corrected that in the code I posted here.
As for the scaling, you can certainly divide the dB values by 10 to get them to go from -100 to 0. However, this significantly distorts your amplitude information, and the units are no longer dB.
See if these changes improve your results.

Connectez-vous pour commenter.

Plus de réponses (2)

Patrik Soukup
Patrik Soukup le 26 Mar 2018

0 votes

Hallo sir, I corrected everything that caused problems and the code works now, another problem I´m facing is windowing. I decided to use either Hann or Hamming window to filter my signal from spectral leakage, but the example I´ve found in help is far from what I was expecting. The syntax is w=hann(L);, where L is a positive integer calculated by L=N+1, where N is what I dont understand. Let´s say I´m using code similar to the above and I get an amount of magnitudes in freq. domain and I would like to apply, let´s say Hann window, where shall I start? Seen the equation and variables in help, yet I can´t understand.
Would appreciate any insight.
Regards, Patrik

1 commentaire

Star Strider
Star Strider le 26 Mar 2018
The ‘L’ parameter is the length of the Hann window, in units of samples. (The purpose of the window functions is to reduce the effects of using a finite sequence to represent an infinite sequence.)
I use windows to design filters, and do not generally use them to calculate Fourier transforms, because they significantly distort the transformed data. My signal processing references only discuss them in the context of filters.
As I read my references, ‘L’ should be the length of your signal. See for example hanning ,hamming window in matlab? (link).

Connectez-vous pour commenter.

Patrik Soukup
Patrik Soukup le 5 Avr 2018

0 votes

Dear Star Strider,
I would like to ask you if you could help me to add some kind of marker to hightlight an area I want to emphasize. I´m trying to plot a signal in frequency domain via below and I would like to add some thin line (or something like that) on...lets say 49,4 Hz and on about 20 other frequency values. There´s probably a way to draw it line by line, but I´m looking for some easier way, because I´m about to do the same on about 10 other plots. I would highly appreciate any help.
Regards,
Patrik

3 commentaires

I have no idea what you want.
Try this:
x = 0:99; % Create Data
y = rand(1,100); % Create Data
vert_lines = randi(99, 1, 5); % Vertical Line Location X-Values
figure
plot(x, y)
hold on
plot([vert_lines; vert_lines], repmat(ylim', 1, numel(vert_lines)), '-r', 'LineWidth',2)
hold off
grid
Patrik Soukup
Patrik Soukup le 6 Avr 2018
Modifié(e) : Patrik Soukup le 6 Avr 2018
Thanks for answer sir,
I gave you insufficient information and I´m sorry for that. I´m really trying to plot a vertical line on certain value of frequency, such as you suggested. In the meantime, I got to this kind of code
line([x x], get(gca, 'ylim'),'Color',[1 0 0],'LineWidth',0.35);
and it works fine,One thing I´m wondering about it the width of the line, it seems to me like the line can´t be thiner than 1, am I right? If I´m not, is there any way to make the line thiner than 1? Tried 0.35 but it looks the same as 1, when used width=2, there was a significant difference and the line was thicker.
Regards,
Patrik
Star Strider
Star Strider le 6 Avr 2018
The default line width is 0.5 points. It may be that it is not possible to define a line width less than that. I cannot find that specific information in the documentation, so I do not know for sure.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Get Started with Signal Processing Toolbox dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by