I need to create a guitar tuner with simulink, which is running on the raspberry pi, but i encountered several problems.
first of all, i got this code to analyze the frequency of a signal:
Fs = 96000;
t = linspace(0,1,Fs);
x = cos(2*pi*4999*t)+randn(size(t));
xdft = fft(x);
fftabs = abs(xdft(1:length(x)/2+1));
freq = 0:Fs/length(x):(Fs/2);
[~,maxindex] = max(fftabs);
mx = freq(maxindex);
res = fftabs;
plot(fftabs);
fprintf('Frequency: %s Hz\n',mx );
and it is doing very well, when i run it, i get the expected result "4999".
but when i am replacing the "x" with the buffered output of the "Audio Device Reader"-Block, i get some strange results or my result equals 0.
In the following image you can see my current model:
I am currently testing on windows, so thats why i am not using the "ALSA Audio Capture" block.The MATLAB Function contains following code:
function res = fcn(x)
Fs = 96000;
xdft = fft(x);
fftabs = abs(xdft(1:length(x)/2+1));
freq = 0:Fs/length(x):(Fs/2);
[~,maxindex] = max(fftabs);
mx = freq(maxindex);
res = fftabs;
fprintf('Frequency: %s Hz\n',mx );
end
The next picture contains a visualisation of the input and output from the Matlab Function
The input was a 440 Hertz tone, but as max value i still get 656hz.
Any ideas, what is causing my problems? Thanks in advance Mick
(I am currently testing on windows, so thats why i am not using the "ALSA Audio Capture" block.)