Finding the frequency value of a signal
Mostrar comentarios más antiguos
How can I find the frequency of this signal?
Thanks,

2 comentarios
Image Analyst
el 26 de Oct. de 2021
@Yasemin G try this:
% Initialization steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
%==================================================================================
% Image Analyst's code below:
period = 0.57e5
originalFrequency = 1/period
x = linspace(0, 2e5, 2000);
signalAmplitude = 0.85;
perfectSineWave = signalAmplitude * sin(2 * pi * (x - 0.08e5) / period);
subplot(2,1,1);
plot(x, perfectSineWave, 'b-');
grid on;
noiseAmplitude = 0.05;
yNoisy = perfectSineWave + noiseAmplitude * (2 * rand(1, length(x)) - 1);
hold on;
darkGreen = [0, 0.5, 0];
plot(x, yNoisy, '-', 'Color', darkGreen)
yline(0, 'Color', 'b', 'LineWidth', 2)
xlabel('Time or x', 'FontSize',fontSize);
ylabel('Signal', 'FontSize',fontSize);
title('Original Signal', 'FontSize',fontSize);
%==================================================================================
% Harry's code below:
% Assume we capture 8192 samples at 1kHz sample rate
Nsamps = 8192;
fsamp = 1000;
Tsamp = 1/fsamp;
t = (0:Nsamps-1)*Tsamp;
% Choose FFT size and calculate spectrum
Nfft = 1024;
[Pxx,f] = pwelch(yNoisy, gausswin(Nfft), Nfft/2, Nfft,fsamp);
% Plot frequency spectrum
subplot(2,1,2);
plot(f, Pxx, 'b-', 'LineWidth', 2);
ylabel('PSD', 'FontSize',fontSize);
xlabel('Frequency (Hz)', 'FontSize',fontSize);
grid on;
% Get frequency estimate (spectral peak)
[~,loc] = max(Pxx);
FREQ_ESTIMATE = f(loc)
caption = sprintf('Frequency estimate = %f Hz', FREQ_ESTIMATE);
title(caption, 'FontSize',fontSize);
g = gcf;
g.WindowState = 'maximized'

Respuesta aceptada
Más respuestas (2)
Image Analyst
el 25 de Oct. de 2014
1 voto
I'd smooth it a bit with a 3rd order Savitzky-Golay filter, sgolayfilt() in the Signal Processing Toolbox, then I'd use findpeaks to get the period and 1/period is the frequency. Attached is a Savitzky-Golay filter demo.

4 comentarios
Ramo
el 26 de Oct. de 2014
Image Analyst
el 26 de Oct. de 2014
Please attach your signal data and code. You must not have smoothed it properly or used good parameters to findpeaks(). Try increasing the window length in sgolayfilt() until your signal doesn't have any noise.
Ramo
el 26 de Oct. de 2014
Image Analyst
el 27 de Mzo. de 2020
Ramo, You chose a frame length, 41, that was far too small. It should be hundreds or thousands because you have 200 thousand data points (which is far too many to see on a single screen without zooming, by the way). Here is corrected code:
% Read in data.
data = dlmread('uapp.txt');
x = data(:, 1);
y = data(:, 2);
% Plot original noisy data.
subplot(1, 3, 2);
plot(x, y, '-');
title('Noisy Data', 'FontSize', 15);
grid on;
% Smooth the data
smoothedY = sgolayfilt(y, 4, 2001);
% Plot smoothed data.
subplot(1, 3, 3);
plot(x, smoothedY, 'r-', 'LineWidth', 2);
grid on;
title('Smoothed Data', 'FontSize', 15);
% Plot both original and smoothed data.
subplot(1, 3, 1);
plot(x, y, '-');
title('Noisy Data', 'FontSize', 15);
grid on;
hold on;
plot(x, smoothedY, 'r-', 'LineWidth', 2);
title('Both Noisy and Smoothed Data', 'FontSize', 15);
% Maximize the window.
g = gcf;
g.WindowState = 'maximized'

Ramo
el 26 de Oct. de 2014
0 votos
7 comentarios
Harry
el 26 de Oct. de 2014
Please upload your data file to this thread and I will modify my code to work with your data.
Ramo
el 26 de Oct. de 2014
Harry
el 26 de Oct. de 2014
Ah okay, so the entire capture is just a few sine periods long. That's very short for an FFT-based method, since the resolution of the raw FFT is fsamp/Nfft (i.e. 10kHz resolution with your data).
I modified my code for your data and it gives an estimate of 40kHz. However, as I said, this is only accurate to the nearest 10kHz at best. Clearly, you could make an equally accurate estimate just by inspecting your graph (in your first post). Therefore, we should try a different approach.
One way is to launch Matlab's Curve Fitting Tool by typing:
sftool
Then, we type:
load uapp.txt
t = uapp(:,1);
x = uapp(:,2);
Then, inside the Curve Fitting Tool, we set "X data" and "Y data" as t and x. Finally, we select a "Sum of Sine" curve type, with "Number of terms" = 1. The result is:

The number we are interested in is the "b1 = 2.22e+05" on the left hand side. This means that the frequency estimate is:
2.22e+05/(2*pi) = 35332.3973664008 Hz
This should be significantly more accurate than the FFT-based method.
Ramo
el 28 de Oct. de 2014
I must reiterate that a basic FFT-based method is a very poor approach for such a short data capture (relative to the period of the sinewave), since it gives a very inaccurate result. You can even get a more accurate result just by looking at the graph and saying the period between the first peak and the second peak is about (40.2μs-12μs) = 28.2μs. Therefore, the frequency is about 1/28.2μs = 35.461kHz.
Instead, we can automate the curve-fitting method like this:
close all; clear all; clc;
% Load data
load uapp.txt
t = uapp(:,1);
x = uapp(:,2);
% Get sine fit
F = fit(t, x, 'sin1');
% Plot result
plot(F,t,x);
grid on;
xlabel('Time (secs)');
ylabel('Amplitude');
title(['Estimated frequency = ', num2str(F.b1/(2*pi)), ' Hz']);
This gives us a frequency estimate of about 35.33kHz:

Ramo
el 30 de Oct. de 2014
Rodrigo Picos
el 27 de Mzo. de 2020
Many years later.....
You should use 'sin3' or 'sin4', and check if you need to use the third or fourth component.
Hint: call F=fit( ) without the ending ;
Categorías
Más información sobre Spectral Estimation en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

