필터 지우기
필터 지우기

How can I change the sampling frequency of audio signal?

조회 수: 34 (최근 30일)
nada fady
nada fady 2015년 6월 13일
답변: Ayush Bhandarkar 2023년 1월 25일
Hi,
I am working on a speech recognition , and am aiming to change the sampling frequency of the audio signal. I wrote the following code :
clear y Fs
%Read the data to the MATLAB using audioread.
[y,fs] = audioread(filename);
%Play the audio.
sound(y,fs);
%change the sampling rate
fs2= fs/2;
audiowrite(filename)
%Read the data back into MATLAB using audioread.
[y,fs2] = audioread(filename);
sound(y,fs2);
when I run it I heard the sound twice in different FS together, But the problem is the original sound has been changed because I wrote on it (audiowrite (filename)).
So can some one help me to change the Fs without changing the original one and compare the results by sound instruction and plot instruction .

채택된 답변

Walter Roberson
Walter Roberson 2015년 6월 13일
audiowrite(filename) is going to fail because you must provide at least 3 parameters to audiowrite: filename, samples, and output frequency.
If you do not want to overwrite the original file then supply a different file name. For example,
newfile = tempname();
audiowrite(newfile, y, fs2);
If all you are going to do with it is read it back in again, then it is pointless to do so: you are just going to get y and fs2 back again. audiowrite() does not resample the data: it just writes the frequency in the header, and whatever tool you use to play the sound is responsible for taking care of the frequency. For example,
sound(y, fs);
sound(y, fs/2);
You probably want resample(), or fft() then ifft(), or interp1()
  댓글 수: 7
madhushankar BS
madhushankar BS 2020년 12월 4일
But the number of samples is not reducing if we are halving the sampling frequency. Why?
Walter Roberson
Walter Roberson 2020년 12월 4일
There are a few different things that people sometimes want to do:
  • keep the samples exactly the same, but change the frequency. For example, slowing down a recording of a person who is talking fast to make it easier to understand. This involves keeping the samples the same but telling the hardware to use a different frequency; the amount of time the signal pertains to changes
  • drop or interpolate samples, to try to construct what the signal would have been like if it had lasted the same amount of time but had been recorded at a different sample rate; resample() is a good tool for that
  • raise or lower the "key" someone is singing or talking at, "pitch conversion", "autotune". This can involve interpolating samples and changing the frequency.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2015년 6월 13일
[y,fs] = audioread(filename);
%change the sampling rate
fs2= 2*fs/3;
audiowrite(filename,y,fs2)
%Read the data back into MATLAB using audioread.
[y,fs2] = audioread(filename);
sound(y,fs2);
  댓글 수: 1
nada fady
nada fady 2015년 6월 13일
편집: nada fady 2015년 6월 13일
Thank you Azzi Abdelmalek to replay
I wrote exactly the same code, but , as i mentioned above the original sound has been changed . is there a way to keep both frequencies and compare them? and how can i plot them?

댓글을 달려면 로그인하십시오.


Ayush Bhandarkar
Ayush Bhandarkar 2023년 1월 25일
I guess this should work,
[y,fs] = audioread(filename);
left = (:,1);
fs2 = fs/2;
fs3 = fs*2;
% soundsc(left,fs2);
soundsc(left,fs3);

카테고리

Help CenterFile Exchange에서 Simulation, Tuning, and Visualization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by