The perfect cup of coffee should made with water high in magnesium and low in bicarbonate, researchers have said.
Scientists have found that the taste of coffee is affected by the mineral content of the water.
Experts used sophisticated chemistry methods to examine the impact of different compositions of water on the flavour of coffee.
They found water composition made a "dramatic difference" to coffee from the same bean – with certain types of hardness boosting the taste.
High magnesium ion levels were found to improve the process of extracting the coffee flavour from the bean, though high bicarbonate levels were bad for flavour.
Sodium rich water, such as that produced by water softeners, was also not beneficial to taste, according to chemist Christopher Hendon of the University of Bath.
Many bottled mineral waters are also relatively high in sodium.
Mr Hendon embarked on the project with his friend, local coffee shop owner Maxwell Colonna-Dashwood, who was crowned the UK's Barista Champion in April.
They will now represent the UK in the World Barista Championships in Rimini, Italy.
Mr Hendon, a PhD student, said: "Coffee beans contain hundreds of chemicals; the precise composition depends on the type of bean and how it is roasted.
"The flavour of the resulting coffee is determined by how much of these chemicals are extracted by the water, which is influenced by roast profile, grind, temperature, pressure and brew time.
"We've found that the water composition is key to the proportions of sugars, starches, bases and acids extracted from a particular roast."
Mr Hendon used computational chemistry methods in the study, published in the Journal of Agricultural Food Chemistry.
These methods were used to examine how different compositions of water affect the extraction of six chemicals that contribute to the flavour of coffee, along with caffeine.
Mr Hendon added: "Hard water is generally considered to be bad for coffee, but we found it was the type of hardness that mattered – while high bicarbonate levels are bad, high magnesium ion levels increase the extraction of coffee into water and improve the taste.
"There is no one particular perfect composition of water that produces consistently flavoursome extractions from all roasted coffee. But magnesium-rich water is better at extracting coffee compounds and the resultant flavour depends on the balance between both the ions in the water and the quantity of bicarbonate present."
Mr Colonna-Dashwood, who owns Colonna and Small's coffee shop in Bath, Somerset, and is a co-author on the paper, said starting with a "reasonably hard" tap water achieved the best results.
"Unfortunately most of the time you are limited by the source water available. Water from the tap varies regionally and from day to day depending on how much it rains," Mr Colonna-Dashwood said.
"Bottled water can offer a consistent water source but none seem to offer the type of water we really want. The best results are reached through utilising different filtration methods and manipulating a reasonably hard tap water.
"For the Championships we test the local water and then select the roast that is most suitable for that particular water. For example, you could use a heavy roast with a soft water as it doesn't extract very much, but with hard water it would extract too much and give a bitter taste, so it would be better to use a lighter roast.
"Traditionally the coffee-making industry is most concerned about using water that doesn't scale up their machines. But we argue that more value should be placed on the flavour of the coffee and want to use chemistry to help people make the best coffee they can with the water they have available."
Mr Colonna-Dashwood will represent the UK at the World Championships, which takes place over four days and involves baristas from 50 countries, from June 9.
Each competitor must prepare four espressos, four cappuccinos and four original signature drinks to exacting standards in a 15-minute performance set to music.
The entries are judged on the taste of the beverage served, cleanliness, creativity, technical skill and overall presentation.
Mr Hendon and Mr Colonna-Dashwood are now planning to write a book on the science behind making the perfect coffee.